如何在ggplot2

时间:2019-07-16 13:38:30

标签: r ggplot2 bar-chart percentage

我有一个从SPSS导入到R中的数据框,但是我无法在ggplot2百分比条形图中正确设置y轴的格式。

我需要将轴范围限制为较小的值,并且还要加长这些条。这就是我不断得到的东西:

enter image description here

以下是上述可视化的代码:

#import packages
library(foreign)
library(ggthemes)
library(stringr)
library(ggplot2)
library(scales)

#read in data
WBGC <- read.spss("2019.07.14_Cleaned.Data.sav", use.value.label=TRUE, to.data.frame=TRUE)
#define member/non-member datasets
WBGC_members <- subset(WBGC, Freq.Of.Attendance == 'Once a month' | Freq.Of.Attendance == 'A few times a month' | Freq.Of.Attendance == 'Once or twice a week' | Freq.Of.Attendance == '3-5 days a week')

#visualization of race
student_race <- ggplot(data = WBGC_members, aes(x = Race, fill = Gender)) 
+ theme_hc()
+ geom_bar(colour = "black", stat = "count", aes(y = prop.table(stat(count))), position = position_dodge(), size = 0.5) 
+ labs(title = "Student Race", y = "Frequency") 
+ scale_y_continuous(labels = percent) 
+ geom_label(data = WBGC_members, stat = 'count', aes(label = scales::percent(prop.table(stat(count))), vjust = -0.4, fontface = 'bold'), size = 6, position = position_dodge(0.9), alpha = 1.0, show.legend = FALSE) 
+ theme(
  plot.title = element_text(size = 16, face = 'bold', family = '', color = 'black', hjust = 0.5, lineheight = 1.2), 
  axis.title.x = element_blank(), 
  axis.text.x = element_text(size = 12, angle = 45, vjust = 0.5),
  axis.title.y = element_text(size = 14, margin = margin(t = 0, r = 8, b = 0, l = 0)),
  axis.text.y = element_text(size = 12),
  legend.title = element_text(size = 14, color = "black", face="bold", hjust = 1, lineheight = 4),
  legend.text = element_text(size = 13),
  legend.position = 'right',
  legend.box.background = element_rect(colour = 'black')
  )
student_race

由于标签看起来正常,因此我在scales::percent的{​​{1}}参数中添加了aes y=,不得不删除了geom_bar函数。我为此感到震惊:

percentage plot attempt 2

非常感谢您的帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

通过在y = prop.table(stat(count))函数调用中添加geom_label来解决。

结果如下:

enter image description here

最后的代码供参考:

student_race <- ggplot(data = WBGC_members, aes(x = Race, fill = Gender)) +
  theme_hc()+
  geom_bar(colour = "black", stat = "count", aes(y = prop.table(stat(count))), position = position_dodge(), size = 0.5) +
  geom_label(data = WBGC_members, stat = 'count', aes(label = scales::percent(prop.table(stat(count))), y = prop.table(stat(count)), vjust = -0.4, fontface = 'bold'), size = 6, position = position_dodge(0.9), alpha = 1.0, show.legend = FALSE) +
  labs(title = "Student Race", y = "Frequency") +
  scale_y_continuous(labels = scales::percent, limits = c(0,0.2)) + 
  theme(plot.title = element_text(size = 16, face = 'bold', family = '', color = 'black', hjust = 0.5, lineheight = 1.2),
        axis.title.x = element_blank(),
        axis.text.x = element_text(size = 12, angle = 45, vjust = 0.5),
        axis.title.y = element_text(size = 14, margin = margin(t = 0, r = 8, b = 0, l = 0)),
        axis.text.y = element_text(size = 12),
        legend.title = element_text(size = 14, color = "black", face="bold", hjust = 1, lineheight = 4),
        legend.text = element_text(size = 13),
        legend.position = 'right',
        legend.box.background = element_rect(colour = 'black')
  )
student_race