繁体   English   中英

如何更改ggplot饼图上的图例值?

[英]How to change legend values on ggplot pie chart?

我在 R 中构建了这个特定的表,以使用 ggplot package 构建饼图。

column,value,proportion,ypos,end,start,middle,hjust,vjust,legend
Urban,268053,97.78067170794165,48.890335853970825,6.143740798014896,0,3.071870399007448,0,1,Urban - 97.78 %
Rural,1296,0.4727563225686427,98.01704986922597,6.173444953813291,6.143740798014896,6.158592875914094,1,0,Rural - 0.47 %
Suburban,4788,1.746571969489708,99.12671401525515,6.283185307179586,6.173444953813291,6.228315130496439,1,0,Suburban - 1.75 %

但是,我希望图例列中的所有值而不是列列都打印在图例中,因为如果我决定在饼图中对它们进行 label,则图例变量太拥挤了。 我也想更改图例标题。

在此处输入图像描述

下面是我用来构建这个图表的代码。

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),      # Adjust so labels are not cut off
                     name = "", breaks = NULL, 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),      # Adjust so labels are not cut off
                     name = "", breaks = NULL, 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"))

Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM