0

我正在尝试在ggplot2中的成对堆叠条形图中的每个条形上方添加总计数(数据框中的总和,在下面的代码中提供)。我附上了一张在 RStudio 中生成的图表图像。例如,在“整体”中的“女性”和“男性”栏上方,应该分别有 1892 和 13334。

另外,如果你看一下图片,标签“2.7%”对于条形来说太大了,我想把那个去掉。我已经尝试了很多东西,但没有任何效果。下面是完全重现我所拥有的图像和代码。

R-图

# ----------------Creating the dataframe-----------------------
Productivity <- c('<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', 
                  '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10')

Period <- c('Overall', 'Overall', 'Overall', 'Overall', 'Starting at 1980', 'Starting at 1980', 
            'Starting at 1980', 'Starting at 1980', 'Starting at 1990', 'Starting at 1990', 
            'Starting at 1990', 'Starting at 1990', 'Starting at 2000', 'Starting at 2000', 
            'Starting at 2000', 'Starting at 2000', 'Overall', 'Overall', 'Overall', 'Overall', 
            'Starting at 1980', 'Starting at 1980', 'Starting at 1980', 'Starting at 1980', 
            'Starting at 1990', 'Starting at 1990', 'Starting at 1990', 'Starting at 1990', 
            'Starting at 2000', 'Starting at 2000', 'Starting at 2000', 'Starting at 2000')

Gender <- c('Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 
            'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Male', 'Male', 'Male', 'Male', 'Male', 
            'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male')

Frequency <- c(1316, 261, 156, 159, 152, 17, 5, 14, 324, 52, 24, 65, 829, 189, 127, 80, 7663, 2041, 1412, 2218, 
               962, 161, 107, 411, 2101, 487, 303, 925, 4332, 1345, 973, 748)

Percentage <- c(69.6, 13.8, 8.2, 8.4, 80.9, 9, 2.7, 7.4, 69.7, 11.2, 5.2, 14, 67.7, 15.4, 10.4, 6.5, 57.5, 15.3, 10.6, 
                16.6, 58.6, 9.8, 6.5, 25, 55.1, 12.8, 7.9, 24.2, 58.6, 18.2, 13.2, 10.1)

Sum <- c(1892, 1892, 1892, 1892, 188, 188, 188, 188, 465, 465, 465, 465, 1225, 1225, 1225, 1225, 13334, 
         13334, 13334, 13334, 1641, 1641, 1641, 1641, 3816, 3816, 3816, 3816, 7398, 7398, 7398, 7398)

Label <- c('69.6%', '13.8%', '8.2%', '8.4%', '80.9%', '9%', '2.7%', '7.4%', '69.7%', '11.2%', '5.2%', 
           '14%', '67.7%', '15.4%', '10.4%', '6.5%', '57.5%', '15.3%', '10.6%', '16.6%', '58.6%', '9.8%', 
           '6.5%', '25%', '55.1%', '12.8%', '7.9%', '24.2%', '58.6%', '18.2%', '13.2%', '10.1%')

d <- data.frame(Productivity, Period, Gender, Frequency, Percentage, Sum, Label)

#--------------Code to produce ggplot graph------------------------------

#Reordering labels
o<-c("<1", "1-5", "6-10", ">10")   
d$ReOrder<-factor(d$Productivity, levels=o)

#Producing plot
p <- ggplot(data=d, aes(x=Gender, y=Frequency, fill=ReOrder, label=Label)) + 
  geom_bar(stat="identity", color="black", position = position_fill(reverse = TRUE)) +
  scale_fill_brewer(palette='Pastel1') +
  geom_text(size = 4, position = position_fill(vjust = 0.5, reverse = TRUE)) +
  facet_grid(~Period) + 
  labs(title="Research productivity", x="", y="Percent", fill="Research longevity (years)") + 
  theme_minimal()+
  theme(plot.title = element_text(size=25, margin=margin(t=20, b=20))) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  coord_fixed(ratio = 6) +
  scale_y_continuous(expand = c(0, 0))

p
4

1 回答 1

1

你可以在你的数据中换掉"""2.7%"解决过大文本的问题。要在每个条形上方添加总计,您可以添加geom_text(aes(x = Gender, y = 1.05, label = as.character(Sum)), vjust = 1)到 ggplot。所以它会是这样的:

library(ggplot2)
library(scales)

# ----------------Creating the dataframe-----------------------
Productivity <- c('<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', 
                  '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10', '<1', '1-5', '6-10', '>10')

Period <- c('Overall', 'Overall', 'Overall', 'Overall', 'Starting at 1980', 'Starting at 1980', 
            'Starting at 1980', 'Starting at 1980', 'Starting at 1990', 'Starting at 1990', 
            'Starting at 1990', 'Starting at 1990', 'Starting at 2000', 'Starting at 2000', 
            'Starting at 2000', 'Starting at 2000', 'Overall', 'Overall', 'Overall', 'Overall', 
            'Starting at 1980', 'Starting at 1980', 'Starting at 1980', 'Starting at 1980', 
            'Starting at 1990', 'Starting at 1990', 'Starting at 1990', 'Starting at 1990', 
            'Starting at 2000', 'Starting at 2000', 'Starting at 2000', 'Starting at 2000')

Gender <- c('Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 
            'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Female', 'Male', 'Male', 'Male', 'Male', 'Male', 
            'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male')

Frequency <- c(1316, 261, 156, 159, 152, 17, 5, 14, 324, 52, 24, 65, 829, 189, 127, 80, 7663, 2041, 1412, 2218, 
               962, 161, 107, 411, 2101, 487, 303, 925, 4332, 1345, 973, 748)

Percentage <- c(69.6, 13.8, 8.2, 8.4, 80.9, 9, 2.7, 7.4, 69.7, 11.2, 5.2, 14, 67.7, 15.4, 10.4, 6.5, 57.5, 15.3, 10.6, 
                16.6, 58.6, 9.8, 6.5, 25, 55.1, 12.8, 7.9, 24.2, 58.6, 18.2, 13.2, 10.1)

Sum <- c(1892, 1892, 1892, 1892, 188, 188, 188, 188, 465, 465, 465, 465, 1225, 1225, 1225, 1225, 13334, 
         13334, 13334, 13334, 1641, 1641, 1641, 1641, 3816, 3816, 3816, 3816, 7398, 7398, 7398, 7398)

Label <- c('69.6%', '13.8%', '8.2%', '8.4%', '80.9%', '9%', '', '7.4%', '69.7%', '11.2%', '5.2%', 
           '14%', '67.7%', '15.4%', '10.4%', '6.5%', '57.5%', '15.3%', '10.6%', '16.6%', '58.6%', '9.8%', 
           '6.5%', '25%', '55.1%', '12.8%', '7.9%', '24.2%', '58.6%', '18.2%', '13.2%', '10.1%')

d <- data.frame(Productivity, Period, Gender, Frequency, Percentage, Sum, Label)

#--------------Code to produce ggplot graph------------------------------

#Reordering labels
o<-c("<1", "1-5", "6-10", ">10")   
d$ReOrder<-factor(d$Productivity, levels=o)

dat <- unique(d[, c("Period", "Gender", "Sum", "ReOrder")])

#Producing plot
p <- ggplot(data=d, aes(x=Gender, y=Frequency, fill=ReOrder, label=Label)) + 
  geom_bar(stat="identity", color="black", position = position_fill(reverse = TRUE)) +
  scale_fill_brewer(palette='Pastel1') +
  geom_text(size = 4, position = position_fill(vjust = 0.5, reverse = TRUE)) +
  facet_grid(~Period) + 
  labs(title="Research productivity", x="", y="Percent", fill="Research longevity (years)") + 
  theme_minimal()+
  theme(plot.title = element_text(size=25, margin=margin(t=20, b=20))) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  coord_fixed(ratio = 6) +
  scale_y_continuous(expand = c(0, 0), labels = percent) +
  geom_text(aes(x = Gender, y = 1.05, label = as.character(Sum)), vjust = 1)

p

在此处输入图像描述

于 2018-04-19T21:21:09.240 回答