如何解决如何更改ggplot饼图上的图例值?
我已经在R中构建了此特定表,以使用ggplot包构建饼图。
column,value,proportion,ypos,end,start,middle,hjust,vjust,legend
Urban,268053,97.78067170794165,48.890335853970825,6.143740798014896,3.071870399007448,1,Urban - 97.78 %
Rural,1296,0.4727563225686427,98.01704986922597,6.173444953813291,6.158592875914094,Rural - 0.47 %
Suburban,4788,1.746571969489708,99.12671401525515,6.283185307179586,6.228315130496439,Suburban - 1.75 %
但是,我希望图例列中的所有值而不是列列中的所有值都被打印在图例中,因为如果我决定在饼图中标记图例变量,它们将太局促。我还想更改图例标题。
下面是我用来构建该图的代码。
library(ggplot2)
library(readr)
library(readxl)
demographics <- read_csv("C:\\path to file\\demographics.csv")
ggplot(demographics) +
geom_arc_bar(aes(x0 = 0,y0 = 0,r0 = 0,r = 1,start = start,end = end,fill = column)) +
coord_fixed() +
scale_x_continuous(limits = c(-1.5,1.4),# Adjust so labels are not cut off
name = "",breaks = NULL,labels = NULL) +
scale_y_continuous(limits = c(-1,1),labels = NULL) +
scale_color_manual(name = "Community Type",labels = c(legend)) +
ggtitle("Percentage of Population by Type of Community")
有人可以协助我吗?预先感谢。
解决方法
尝试一下:
library(ggplot2)
library(readr)
library(readxl)
library(ggforce)
ggplot(demographics) +
geom_arc_bar(aes(x0 = 0,y0 = 0,r0 = 0,r = 1,start = start,end = end,fill = legend)) +
coord_fixed() +
scale_x_continuous(limits = c(-1.5,1.4),# Adjust so labels are not cut off
name = "",breaks = NULL,labels = NULL) +
scale_y_continuous(limits = c(-1,1),labels = NULL) +
scale_color_manual(name = "Community Type",labels = c(legend)) +
ggtitle("Percentage of Population by Type of Community")+
guides(fill=guide_legend(title="New Legend Title"))
输出:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。