2015-01-10 127 views
0

我被告知最好将子问题分成SE上的多个主题,所以在这里我去了。将条形图的显示值扩展为多变量堆积条形

我已修改the answer in this question以出示a multivariate stacked bar plot。一切工作正常,但值不能正确显示。

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(如this answer提供)。我应该怎么做呢?

+0

您在制表地区,其中有两个层次,那么为什么你期望多于两个值?你是不是指'table(dnom $ Region,dnom $ Variant)'? [** This **](http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2/6645506#6645506)可能是一个相关的帖子。 – Henrik

+0

@亨利克噢我糟糕。修复!我只是不知道如何定位这些值相对于酒吧。我已经看过那篇文章(以及基本上整个互联网上关于它自己的所有栏杆的其他帖子),我仍然无法弄清楚这一点...... –

+0

@Henrik请参阅我的编辑,我试过(再次)申请解决方案,但它不能解决问题。 –

回答

1

我认为在你的链接和你对它的修改之间的翻译之间有些东西丢失了。

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

enter image description here

+0

这适用于“简单”堆积图(+1),但你有任何想法如何应用这[二元多重情节](http://stackoverflow.com/questions/27803031/plottinga-a-二元到多重因素,在-R)? –

+0

看来你设法得到了答案[这里](http://stackoverflow.com/a/27838986/937932)。 – nacnudus