我想得到这个半月图模板的脚本,我在文章中看到https://doi.org/10.3390/plants12071451使用ggplot2。
zlhcx6iw1#
这绝对是非常古怪的,但这里有一个使用ggplot2::coord_polar()的方法:
ggplot2::coord_polar()
library(tidyr) library(dplyr) library(ggplot2) library(scales) set.seed(13) # example data dat <- tibble( id = 1:20, `Grain (%)` = runif(20, .525, .625), `Straw (%)` = 1 - `Grain (%)` ) # parameters for y axis and gridlines panel <- expand_grid( x.lab = c(-.25, 21.25), x.grid = c(0, 21), y = seq(0, .7, by = .1) ) dat %>% mutate(id = reorder(id, `Grain (%)`, decreasing = TRUE)) %>% pivot_longer(!id, names_to = "type", values_to = "pct") %>% ggplot(aes(id, pct)) + geom_line( data = panel, aes(x.grid, y, group = y), linewidth = .1, color = "gray70" ) + geom_text( data = panel, aes(x.lab, y, label = percent(y)), size = 8 / .pt ) + geom_line(aes(group = id), color = "gray50") + geom_point(aes(color = type), size = 3) + scale_x_discrete(expand = expansion(add = c(12, 12))) + scale_y_continuous(limits = c(0, .7), expand = c(0, 0)) + coord_polar(start = pi) + guides(color = guide_legend(direction = "horizontal")) + theme_void() + theme( axis.text.x = element_text(size = 8), panel.grid.major.x = element_line(linewidth = .1, color = "gray70"), legend.position = c(.5, .45), legend.title = element_blank() )
68de4m5k2#
看起来很好玩:D数据(参考@zephryl的回答):
library(ggplot2) library(dplyr) n <- 20 set.seed(13) dat <- tibble::tibble( id = 1:n, Grain = runif(n, .525, .625), Straw = 1 - Grain ) %>% arrange(Straw) %>% mutate(id = forcats::fct_inorder(as.character(id))) %>% mutate(grp = rep(letters, each = 4)[seq_len(n)])
笛卡尔坐标中的解的代码:
library(ggforce) angles <- seq_len(n)/(n+1) * pi dat %>% arrange(desc(id)) %>% mutate(id.pos = max(c(Grain, Straw)) * 1.2, grp.pos = Grain * 1.05, angle = angles, spoke.radius = Grain - Straw) %>% tidyr::pivot_longer(cols = c(Grain, Straw, id.pos, grp.pos)) %>% mutate(x = value * cos(angle), y = value * sin(angle), label = case_when(name == "id.pos" ~ as.character(id), name == "grp.pos" ~ grp, TRUE ~ ""), spoke.radius = ifelse(name == "Straw", spoke.radius, 0)) %>% select(-c(id, grp)) %>% ggplot(aes(x = x, y = y)) + # background geom_arc(data = data.frame(r = seq(0.1, 0.7, by = 0.1), start = -pi/2, end = pi/2), aes(x0 = 0, y0 = 0, r = r, start = start, end = end), inherit.aes = FALSE, color = "grey") + geom_spoke(data = . %>% filter(name == "id.pos"), aes(x = 0, y = 0, angle = angle, radius = 0.7), color = "grey") + annotate("segment", x = -0.7, xend = 0.7, y = 0, yend = 0, color = "black") + annotate("segment", x = seq(-0.7, 0.7, 0.1), xend = seq(-0.7, 0.7, 0.1), y = 0, yend = -0.01, color = "black") + # plot geom_spoke(aes(angle = angle, radius = spoke.radius)) + geom_point(data = . %>% filter(name %in% c("Grain", "Straw")), aes(color = name), size = 4) + # labels geom_text(aes(label = label, fontface = ifelse(name == "grp.pos", "bold", "plain"))) + scale_x_continuous(breaks = seq(-0.7, 0.7, 0.1), labels = function(x) scales::percent_format()(abs(x))) + coord_equal() + theme_void() + theme(legend.position = "bottom", legend.title = element_blank(), axis.text.x = element_text(colour = "black"))
9jyewag03#
你可能想试试plotly::plot_ly(type = 'barpolar'),像这样:
plotly::plot_ly(type = 'barpolar')
library(plotly) p <- plot_ly( type = 'barpolar', r = c(54.7, 32.6, 8.4, 10.4, 30.6), theta = c(0, 40, 80, 120, 160) ) %>% layout( polar = list( radialaxis = list( visible = TRUE, range = c(0, 60) ), sector = c(0, 180) ) ) p
3条答案
按热度按时间zlhcx6iw1#
这绝对是非常古怪的,但这里有一个使用
ggplot2::coord_polar()
的方法:68de4m5k2#
看起来很好玩:D
数据(参考@zephryl的回答):
笛卡尔坐标中的解的代码:
9jyewag03#
你可能想试试
plotly::plot_ly(type = 'barpolar')
,像这样: