将条形图的显示值扩展到多元堆叠条形图

Extend displaying values of bar plot to multivariate stacked bar

有人告诉我在 SE 上最好将子问题分成多个主题,所以我开始了。

我已经适应了 to be presentable in 。一切正常,但值显示不正确。

dnom <- data.frame(Variant = sample(c("iedere","elke"),size = 50,replace = TRUE),
        Region = sample(c("VL","NL"),size = 50,replace = TRUE),
        PrecededByPrep = sample(c("1","0"),size = 50,replace = TRUE),
        Person = sample(c("person","no person"),size = 50,replace = TRUE),
        Time = sample(c("time","no time"),size = 50,replace = TRUE))

ggplot.labs <- data.frame(table(dnom))
# OR?
ggplot.data <- melt(dnom, id.vars = "Variant")
ggplot.data <- ddply(ggplot.labs, .(Var1), transform, pos = cumsum(Freq) - (0.5 * Freq))

ggplot(dnom, aes(x=Variant)) +
        geom_bar(aes(fill=Variant)) + 
        geom_text(data=ggplot.labs, aes(x=Var1, label=Freq, y=Freq/2), size=3) +
        scale_fill_manual(values = c("paleturquoise3", "palegreen3")) +
        theme_corpling() +
        labs(x="Variant", y="Frequentie", title="Distributie van varianten") +
        guides(fill=FALSE)

但是如您所见,我不确定如何合并 ddplymelt(如 中所提供)。我该怎么办?

我认为您链接到的答案和您对它的改编之间的翻译丢失了一些东西。

# Code from an older edit of the question.
dnom_labs <- data.frame(table(dnom$Region, dnom$Variant))

# Suggested answer
Data <- ddply(dnom_labs, .(Var1), transform, pos = cumsum(Freq) - (0.5 * Freq))

ggplot(Data, aes(x=Var1, y = Freq)) +
        geom_bar(aes(fill=Var2), stat = "identity") + 
        geom_text(aes(label = Freq, y = pos), size = 3) +
        labs(x="Region", y="Frequencies", title="Distribution of variant")