R中的复杂ggplot-半圆条形图

时间:2020-08-24 20:49:42

标签: r ggplot2

好的,这就是挑战。如何重新创建此图表?

数字等不必匹配,我真正想做的是在带有间隙的量规类型布局中创建圆形条形图。标头和文本是可选的。仅仅是3/4圆形条形图的想法。

enter image description here

这是我正在使用的一些示例代码:

library(ggplot2)

fixed_income.df <- data.frame(name = c("total","US Gov't Debt","US Municipal Debt",
                                       "US IG Corp","US HY Corp","Int'l Developed",
                                       "Emerging Market"),
                              allocation = c(3,1,4,3,4,2,3),
                              x_ax = c(1:7))




ggplot(fixed_income.df,aes(x = as.numeric(x_ax), y = allocation)) + 
  geom_bar(stat = "identity") + 
  ylim(-5,5) +
  coord_polar(
    theta = "x",
    start=-3)
  ) + coord_flip()

返回:

enter image description here

任何帮助都将获得Cookie!实在没有任何帮助会如此感激,我被困住了。

Sody

2 个答案:

答案 0 :(得分:2)

基本图解的代码非常简单(至少没有注释)

library(ggplot2)

ggplot(df, aes(xvals, yvals, fill = cols)) +
  geom_col(width = 1) +
  scale_y_continuous(limits = c(-2, 3)) +
  scale_fill_manual(values = rev(c("#e9cbc1", "#b54649", "gray90",
                                   "gray50", "#8ba55d", "#e2e4d6",
                                   "white", "#c3a891", "#37959d", 
                                   "#5c7890", "#dcad3c", "#55a3b9",
                                   "#f39068"))) +
  theme_void() +
  geom_vline(colour = "white", xintercept = c(0.5, 1.5, 8.5, 15.5, 16.5, 17.5),
             size = 3) +
  geom_segment(data = data.frame(x = 0.5 + 1:23, y = 0, yend = 1),
               aes(x = x, y = 0, yend = 1, xend = x), colour = "white",    
               inherit.aes = FALSE) +
  scale_x_continuous(expand = c(0.2, 1)) +
  coord_polar(start = -pi) +
  theme(legend.position = "none")

enter image description here

以正确的格式获取数据非常困难:

df <- structure(list(xvals = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L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 3L, 2L, 1L, 13L, 12L, 
11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 13L, 12L, 11L, 
10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 13L, 12L, 11L, 10L, 
9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 13L, 12L, 11L, 10L, 9L, 8L, 
7L, 6L, 5L, 4L, 3L, 2L, 1L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 
5L, 4L, 3L, 2L, 1L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 
3L, 2L, 1L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 
1L), .Label = c("Nesting Variable 6", "Nesting Variable 5", 
"Nesting Variable 4", 
"Nesting Variable 3", "Nesting Variable 2", "Nesting Variable 1", 
"blank", "mint", "green", "darkgray", "lightgray", "red", "pink"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-299L))

答案 1 :(得分:0)

OMG金发时刻,答案是如此简单..我怎么想念它..

xlim()