用ggplot2将R中的值集中在R中 [英] Centering values in R with ggplot2

查看:87
本文介绍了用ggplot2将R中的值集中在R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将这个答案应用于我的数据集,但是 - 作为初学者,它根本不适合我。

以下是一个示例设置和示例代码:

  d < -  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))

qplot(factor(Variant),data = d,geom =bar, fill = Variant)+
theme_bw()+
xlab()+
ylab(Frequencies)+
geom_text(aes(label = factor(Variant),y = 5),size = 3)

这是一个简单的柱状图,只有两个元素条形图应显示iedere和elke的频率)。我尝试添加 geom_text 的值,我认为这个值至少会显示出现的次数,但这不起作用。相反,它会显示标签,而不是数值。



我也想将这个应用于堆积图分组堆积图 - 显示此处。我假设所有三个图表的方法都是相同的,但是我发现我甚至无法得到第一个使用提供的代码的工作,我没有尝试其他两个。有些方向和帮助将不胜感激。

解决方案

您需要为标签创建辅助数据框。我还重构了绘图代码,因为您最终需要做更多的工作,而 qplot 实际上只适用于快速绘图或来自基本绘图的需要快速绘制的绘图:

  d_labs < -  data.frame(table(d $ Variant))

gg< - ggplot(d,aes(x = Variant))
gg< - gg + geom_bar(aes(fill = Variant))
gg< - gg + geom_text(data = d_labs,aes(x (x = NULL,y =Frequencies= Var1,label = Freq),y = 5,size = 3)
gg < - gg + theme_bw()
gg < - gg + labs )
gg



您可以更多地控制 geom_text geoms这种方式。

编辑

您可以将垂直位置映射到 Freq 审美观:

  gg< ;  -  gg + geom_text(data = d_labs,aes(x = Var1,label = Freq,y = Freq / 2),size = 3)


I am trying to apply this answer to my dataset, but - being a beginner, it isn't working out for me at all.

Here is a sample set and sample code:

d <- 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))

    qplot(factor(Variant), data=d, geom="bar", fill=Variant) +
      theme_bw() +
      xlab("") +
      ylab("Frequencies") + 
      geom_text(aes(label = factor(Variant), y = 5), size = 3)

This is a simple bar plot, of only two elements (so the bar plot should show the frequency of "iedere" and "elke"). I tried adding geom_text with a value that I thought would at least show the amount of occurences, but that doesn't work. Instead it shows me the labels, not the values.

I would also like to apply this to stacked plots and a grouped stacked plot - as shown here. I am assuming that the method is the same for all three graphs, but seeing that I can't even get the first one to work with the code provided, I didn't try the other two. Some directions and help would be greatly appreciated.

解决方案

You'll need to create a secondary data frame for the labels. I also restructured the plotting code since you want to eventually do more with it and qplot is really just for "quick" plots or those coming from base plotting who need a quick mapping:

d_labs <- data.frame(table(d$Variant))

gg <- ggplot(d, aes(x=Variant))
gg <- gg + geom_bar(aes(fill=Variant))
gg <- gg + geom_text(data=d_labs, aes(x=Var1, label=Freq), y=5, size=3)
gg <- gg + theme_bw()
gg <- gg + labs(x=NULL, y="Frequencies")
gg

You get way more control over the geom_text geoms this way.

EDIT

You can map the vertical position to the Freq aesthetic:

gg <- gg + geom_text(data=d_labs, aes(x=Var1, label=Freq, y=Freq/2), size=3)

这篇关于用ggplot2将R中的值集中在R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆