我认为这是可行的:
si <- read.csv('sampledata.csv', sep=' ')
myplot <- ggplot(data = si
,aes(DEGREE)
)
myplot <- myplot + geom_bar()
myplot <- myplot + labs(title = "Degree by Urban/Rural", y = "Percent", x = "DEGREE")
myplot <- myplot + geom_text(aes(y = ((..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..]), label = scales::percent((..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..])), stat = "count", vjust = -0.25)
myplot <- myplot + facet_wrap(~URBRURAL)
myplot <- myplot + theme(axis.text.x = element_text(angle = 20, hjust = 1))
myplot
实际上 y 轴标签不是百分比而是实际计数,因为它们在您的原始图中,条形图上的标签代表百分比,请看下面的第 18 行,这表明 45 不是百分比,而是该组的实际计数在您提供的示例数据中,而在相应方面的同一条上的 15.7% 表示百分比。
library(dplyr)
as.data.frame(si %>% group_by(URBRURAL, DEGREE) %>% summarise(n=n()))
1 Country village, other type of community Above higher secondary level, other qualification 6
2 Country village, other type of community Above lowest qualification 16
3 Country village, other type of community Higher secondary completed 9
4 Country village, other type of community Lowest formal qualification 31
5 Country village, other type of community No formal qualification 20
6 Country village, other type of community University degree completed 1
7 Farm or home in the country Above lowest qualification 1
8 Farm or home in the country Higher secondary completed 1
9 Farm or home in the country Lowest formal qualification 5
10 Farm or home in the country No formal qualification 1
11 Farm or home in the country University degree completed 1
12 Suburb, outskirt of a big city Above higher secondary level, other qualification 45
13 Suburb, outskirt of a big city Above lowest qualification 57
14 Suburb, outskirt of a big city Higher secondary completed 75
15 Suburb, outskirt of a big city Lowest formal qualification 48
16 Suburb, outskirt of a big city No formal qualification 23
17 Suburb, outskirt of a big city University degree completed 15
18 Town or small city Above higher secondary level, other qualification 45