【问题标题】:Add custom legend to pie chart将自定义图例添加到饼图
【发布时间】:2021-01-05 06:21:36
【问题描述】:

有没有办法添加自定义图例。现在,图例显示列“a”,我们可以显示列“图例”吗?因为“图例”列既有标签又有值。我们可以这样做吗?:)

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")

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-08
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多