r - 更改填充颜色方向

标签 r ggplot2 polar-coordinates

在这种类型的绘图中,有没有办法将填充颜色的方向从外更改为内(中心)?

Imgur

这是我的代码:

dat <- structure(list(
    Category = structure(1:15,
      .Label = c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6", "ID7", "ID8", "ID9", "ID10", 
         "ID11", "ID12", "ID13", "ID14", "ID15"), class = "factor"),
    Percentage = c(0.8, 0.32, 0.15, 0.6, 0.4, 0.5, 0.3, 0.7, 1, 0.8, 0.3, 0.9, 0, 0.2, 0.46)),
  .Names = c("Category", "Percentage"), row.names = c(NA, 15L), class = "data.frame")

ggplot(data = dat) +
  geom_col(aes(x = Category, fill = Category, y = Percentage), width = 1) +
  geom_hline(yintercept = 1, color = "darkgrey", size = 1) + #ponto de partida
  geom_segment(aes(x = 1.5, xend = 4.5, y = .75, yend = .75), color = "darkgrey", size = 1) +#2017
  geom_segment(aes(x = 5.5, xend = 15.5, y = .75, yend = .75), color = "darkgrey", size = 1) +#2017
  geom_segment(aes(x = 1.5, xend = 15.5, y = .5, yend = .5), color = "darkgrey", size = 1) +#2018
  geom_segment(aes(x = .5, xend = 4.5, y = .25, yend = .25), color = "darkgrey", size = 1) +#2019
  geom_segment(aes(x = 5.5, xend = 15.5, y = .25, yend = .25), color = "darkgrey", size = 1) +#2019
  geom_vline(xintercept = seq(.5, 15.5, by = 1), color = "darkgrey", size = 1) +
  coord_polar() +
  theme_minimal() +
  labs(x = NULL, y = NULL) +
  theme(axis.text.y = element_blank(),
        legend.position = "none",
        panel.grid = element_blank())

最佳答案

您只需添加 scale_y_reverse() 即可让 y 值从外侧开始。 enter image description here

顺便说一句,图表很漂亮,感谢您提供可重现的示例!

ggplot(data = dat) +
  geom_col(aes(x = Category, fill = Category, y = Percentage), width = 1) +
  geom_hline(yintercept = 1, color = "darkgrey", size = 1) + #ponto de partida
  geom_segment(aes(x = 1.5, xend = 4.5, y = .75, yend = .75), color = "darkgrey", size = 1) +#2017
  geom_segment(aes(x = 5.5, xend = 15.5, y = .75, yend = .75), color = "darkgrey", size = 1) +#2017
  geom_segment(aes(x = 1.5, xend = 15.5, y = .5, yend = .5), color = "darkgrey", size = 1) +#2018
  geom_segment(aes(x = .5, xend = 4.5, y = .25, yend = .25), color = "darkgrey", size = 1) +#2019
  geom_segment(aes(x = 5.5, xend = 15.5, y = .25, yend = .25), color = "darkgrey", size = 1) +#2019
  geom_vline(xintercept = seq(.5, 15.5, by = 1), color = "darkgrey", size = 1) +
  scale_y_reverse()+
  coord_polar() +
  theme_minimal() +
  labs(x = NULL, y = NULL) +
  theme(axis.text.y = element_blank(),
        legend.position = "none",
        panel.grid = element_blank())

此外,以高分辨率呈现:

require(Cairo)
ggsave("/path/", height=10,width=10, dpi=300, type="cairo-png")
# change height/width/dpi as necessary

关于r - 更改填充颜色方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48832987/

相关文章:

r - stringdist 包中的 Jaccard 相似度来匹配字符串中的单词

r - 如何混合 2 个或多个调色板以显示组合颜色值

r - 将 purrr 和 ggplot 与 group_by 和 Nest() 结合使用时获取绘图标题

r - 在 ggplot2 中使用自定义 OTF 字体

r - 如何使用 ggplot2 将标签放在 R 中每个条形的 geom_bar 上

ios - 极坐标点生成函数上界不是2Pi for theta吗?

python - Matplotlib - 在极坐标图中画一个光滑的圆

R - 使用正则表达式查找/替换换行符

r - 检查 R 中是否存在街道地址

r - Plotrix R 包 polar.plot 符号覆盖