我尝试在ggplot2中将总计数(数据帧中的总和,在下面的代码中提供)添加到成对堆叠条形图中的每个条形图之上。我附上了该图的图像,这是在RStudio中生成的。例如,在“总体”中的“女性”和“男性”栏的上方,应该分别有1892和13334。
另外,如果你看一下图片,标签"2.7%“对条形图来说太大了,我想把它去掉。我尝试了很多方法,但都不起作用。下面是图像和代码,以完全复制我所拥有的东西。
# ----------------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
发布于 2018-04-20 05:21:09
您可以在数据中用"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
https://stackoverflow.com/questions/49929508
复制相似问题