maj*_*jom 14 plot r ggplot2 pie-chart
有没有办法在R中创建这样的图表?
以下是图表中显示的数据摘录:
df <- structure(list(Animal = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Buffalo",
"Goat", "Sheep"), class = "factor"), Texture = structure(c(4L,
4L, 4L, 4L, 4L, 3L, 3L, 3L, 2L, 1L, 1L, 4L, 3L, 4L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L), .Label = c("Hard", "Semi-Hard", "Semi-Soft",
"Soft"), class = "factor"), Name = structure(c(16L, 9L, 3L, 21L,
5L, 4L, 10L, 2L, 12L, 11L, 8L, 14L, 1L, 7L, 22L, 15L, 6L, 20L,
18L, 17L, 19L, 13L), .Label = c("Buffalo Blue", "Charolais",
"Chevre Bucheron", "Clochette", "Crottin de Chavignol", "Feta",
"Fleur du Maquis", "Garrotxa", "Golden Cross", "Humboldt Fog",
"Idaho Goatster", "Majorero", "Manchego", "Mozzarella di Bufala Campana",
"Ossau-Iraty", "Pantysgawn", "Pecorino Romano", "Pecorino Sardo",
"Roncal", "Roquefort", "Sainte-Maure de Touraine", "Yorkshire Blue"
), class = "factor")), .Names = c("Animal", "Texture", "Name"
), class = "data.frame", row.names = c(NA, -22L))
Run Code Online (Sandbox Code Playgroud)
那张图很棒.
好吧,在我开始有点疯狂之前,我已经做到了这一点.简而言之,有一种快速的非ggplot2方式(几乎可以提供你想要的东西)和一种长的ggplot2方式(它几乎可以满足你的需求).快速方式(使用df
上面给出的示例数据):
devtools::install_github("timelyportfolio/sunburstR")
library(sunburstR)
df1 <- df %>%
group_by(Animal) %>%
unite(col=Type, Animal:Name, sep = "-", remove=T)
df1$Type <- gsub(" ", "", df1$Type)
df1$Index <- 1
sunburst(df1)
Run Code Online (Sandbox Code Playgroud)
这为您提供了一个很棒的交互式图像(这里不是交互式的,只是一个快照):
ggplot2方式很棘手,我还没弄清楚如何在图像上正确注释,但也许有人可以构建这个代码并且这样做.
df1 <- df %>%
mutate(Colour = ifelse(.$Animal == "Goat", "#CD9B1D", ifelse(.$Animal == "Sheep", "#EEC900", "#FFD700"))) %>%
mutate(Index = 1) %>%
group_by(Animal)
Run Code Online (Sandbox Code Playgroud)
有三层:
First <- ggplot(df1) + geom_bar(aes(x=1, y=Animal, fill=Animal,
label = Animal), position='stack', stat='identity', size=0.15)
+ theme(panel.grid = element_blank(), axis.title=element_blank(),
legend.position="none", axis.ticks=element_blank(),
axis.text = element_blank())
Second <- First
+ geom_bar(data=df1, aes(x=2, y=Animal, fill=Texture, group=Animal),
position='stack', stat='identity', size=0.15, colour = "black")
+ scale_color_brewer(palette = "YlOrBr")
+ scale_fill_brewer(palette = "YlOrBr")
+ theme(axis.title=element_blank(), legend.position="none",
axis.ticks=element_blank(), axis.text = element_blank())
Third <- Second + geom_bar(data=df1, aes(x=3, y=Animal, fill=Name),
position='stack', stat='identity', size=0.15, colour = "black")
+ scale_fill_manual(values = c("#EEC900", "#FFD700", "#CD9B1D",
"#FFD700", "#DAA520", "#EEB422", "#FFC125", "#8B6914", "#EEC591",
"#FFF8DC", "#EEDFCC", "#FFFAF0", "#EEC900", "#FFD700", "#CDAD00",
"#FFF68F", "#FFEC8B", "#FAFAD2", "#FFFFE0", "#CD853F", "#EED8AE",
"#F5DEB3", "#FFFFFF", "#FFFACD", "#D9D9D9", "#EE7600", "#FF7F00",
"#FFB90F", "#FFFFFF"))
+ theme(axis.title=element_blank(), legend.position="none",
axis.ticks=element_blank(), axis.text.y = element_blank(),
panel.background = element_rect(fill = "black"))
Third + coord_polar('y')
Run Code Online (Sandbox Code Playgroud)
这给了我们:
好吧,那就像我一样接近.对于任何可以在R中重新创建该图形的人来说,都是严肃的帽子!