将自定义图例添加到饼图

时间:2020-09-18 10:02:09

标签: r ggplot2

是否可以添加自定义图例。现在,图例显示的是“ a”列,而我们可以显示的是“ legend”列吗?因为“传奇”列同时具有标签和值。我们可以做到吗?:)

library('ggplot2')
asd <- data.frame(a = c("fds","fdsf"), b = c(3,4))
asd$legend <- paste0(asd$a,"-",asd$b)
ggplot(asd, aes(x="", y=b, fill = a)) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y")

1 个答案:

答案 0 :(得分:0)

是的,只需将填充映射到legend列:

ggplot(asd, aes(x = "", y = b, fill = legend)) + 
  geom_bar(stat = "identity", width = 1) + 
  coord_polar(theta = "y")

enter image description here