在ggplot2

时间:2018-04-19 20:15:02

标签: r ggplot2 stackedbarseries

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

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

R-plot

# ----------------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

1 个答案:

答案 0 :(得分: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

enter image description here