这是我想要实现的目标:
堆积条形图中的值是计数。他们应该集中。如果计数为8或更少,则不应显示计数。
我到目前为止:
数据
str(exampledata$V43)
Factor w/ 5 levels "5 Sehr unzufrieden",..: 3 5 4 4 4 4 4 5 5 4 ...
str(exampledata$A_REF)
Factor w/ 18 levels "Zertifikat eines Aufbau- oder Ergänzungsstudiums",..: 18 18 18 18 18 17 18 18 18 18 ...
str(exampledata$V101)
Factor w/ 2 levels "Weiblich","Männlich": 2 NA 2 2 2 2 1 1 1 2 ...
dput(df[1:100,])
structure(list(exampledata.V101 = structure(c(2L, NA, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, NA, 2L, 2L, 2L, 1L, 2L, NA,
NA, NA, 1L, 1L, 2L, NA, 2L, 2L, 2L, NA, 2L, 2L, NA, NA, 1L, NA,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, NA, NA, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, NA, 1L, NA, 1L, NA,
1L, 2L, NA, NA, 2L, NA, 1L, 2L, 2L, NA, 2L, NA, 2L, 2L, 1L, 2L,
1L, 2L, 1L, 1L, 2L, 1L, NA, 2L, 2L, 2L, 2L, NA, 2L, 1L, 2L, 2L
), .Label = c("Weiblich", "Männlich"), class = "factor"), exampledata.A_REF = structure(c(18L,
18L, 18L, 18L, 18L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 16L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 16L, 18L, 18L, 16L, 18L,
16L, 18L, 18L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
16L, 18L, 18L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 17L, 16L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 17L, 18L, 18L,
16L, 18L, 16L, 18L, 18L, 16L, 16L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 16L, 18L,
16L, 16L, 18L, 18L, 18L, 17L, 16L, 18L), .Label = c("Zertifikat eines Aufbau- oder Ergänzungsstudiums",
"LA Berufliche Schulen", "LA Sonderschule", "LA Gymnasium", "LA Haupt- und Realschule",
"LA Grundschule", "Künstlerischer/musischer Abschluss", "Kirchlicher Abschluss",
"Staatsexamen (ohne Lehramt)", "Diplom Fachhochschule, Diplom I an Gesamthochschulen",
"Diplom Universität, Diplom II an Gesamthochschulen", "Sonstiges",
"Promotion", "Staatsexamen", "Magister", "Diplom", "Master",
"Bachelor"), class = "factor"), exampledata.V43 = structure(c(3L,
5L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 4L, 3L, 3L, 2L, NA, 4L, 5L, 5L,
4L, 4L, 4L, 4L, NA, 2L, 4L, 3L, 5L, 4L, 4L, 4L, NA, 4L, 4L, NA,
NA, 3L, 5L, 2L, 4L, 5L, 4L, 4L, 5L, 5L, 4L, NA, NA, 4L, NA, 3L,
4L, 5L, 5L, 2L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 5L, 4L, 5L, NA, 4L,
NA, 4L, NA, 4L, 5L, 4L, NA, 5L, NA, 4L, 4L, 4L, NA, 4L, NA, 5L,
4L, 4L, 4L, 4L, 4L, 3L, 3L, 4L, 2L, 4L, 4L, 4L, 3L, 4L, NA, 4L,
5L, 5L, 4L), .Label = c("5 Sehr unzufrieden", "4", "3", "2",
"1 Sehr zufrieden"), class = "factor")), .Names = c("exampledata.V101",
"exampledata.A_REF", "exampledata.V43"), row.names = c(NA, 100L
), class = "data.frame")
剧情
ggplot(data=subset(subset(exampledata, !is.na(V101)), !is.na(V43)), aes(x=A_REF, fill=factor(V43))) +
geom_bar(position="fill") +
facet_grid(~V101) +
labs(y=NULL, x=NULL, fill=NULL) +
scale_y_continuous(labels = scales::percent, breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1)) +
ggtitle(paste(attr(exampledata, "variable.labels")[77])) +
theme_classic() +
coord_flip()
修改: 我知道有类似的问题,但我无法使解决方案适应我的问题。大部分时间代码都没有用。
EDIT2 : 来自Erdem Akkas的解决方案会导致以下两个错误:
Error in `[.data.frame`(exampledata, !is.na(V101) & !is.na(V43), .(length(.I)), :
unused argument (by = .(A_REF, V101, V43))
Error in combine_vars(data, params$plot_env, cols, drop = params$drop) :
At least one layer must contain all variables used for facetting
ggplot(data=subset(subset(exampledata, !is.na(V101)), !is.na(V43)), aes(x=A_REF, fill=factor(V43))) +
geom_bar(position="fill") +
facet_grid(~V101) +
labs(y=NULL, x=NULL, fill=NULL) +
scale_y_continuous(labels = scales::percent, breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1)) +
ggtitle(paste(attr(exampledata, "variable.labels")[77])) +
theme_classic() +
geom_text(stat="count",aes(label=ifelse((..count..)>0, ..count.., "")), position = position_fill(vjust=0.5)) +
coord_flip()
答案 0 :(得分:2)
我首先用data.table总结数据:
library(data.table)
summary<-exampledata[!is.na(V101) & !is.na(V43),.(length(.I)),by=.(A_REF,V101,V43)]
ggplot(data=summary, aes(x=A_REF,y=V1, fill=factor(V43))) +
geom_bar(position="fill",stat="identity") +
facet_grid(~V101) +
labs(y=NULL, x=NULL, fill=NULL) +
scale_y_continuous(labels = scales::percent, breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1)) +
ggtitle(paste(attr(exampledata, "variable.labels")[77])) +
theme_classic() +
geom_text(aes(label=ifelse(V1>8,V1,"")),hjust=1,position = position_fill())+coord_flip()
`