当使用fun.y ggplot时改变y的比例 [英] changing y scale when using fun.y ggplot

查看:155
本文介绍了当使用fun.y ggplot时改变y的比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据的一个例子

  library(ggplot)
set.seed(1)$ b $ (样本(1:100,50))
df [x,样式(组)=因子(rep(1:10,每个= 10)))
x< - Lame
df [-x,Style]< - Cool
df $ Style < - factor(df $ Style)

p <-ggplot()+ stat_summary(data = df,aes(Groups,Style,fill = Style),
geom =bar,fun.y = length,position = position_dodge())

(对不起,这是我的第一个问题...我不知道如何显示头像代码片段( )



因此,剧情充分呈现每个'组'的每个'风格'的计数, 。但是,y轴刻度显示因子变量'Style'的等级。虽然我绘制的数值本来是离散的,但每个'组'的'Cool'和'Lame'的计数是连续的。

我的barplot从ggplot2中的离散到连续的比例,以对应计数值而不是原始因子水平?

解决方案

您可以利用 ggplot 分组和直方图这个给你

  p < -  ggplot(df,aes(Groups,fill = Style))+ geom_histogram(position = position_dodge ())


This an example of my data

library(ggplot)
set.seed(1)
df <- data.frame(Groups = factor(rep(1:10, each = 10)))
x <- sample(1:100, 50)
df[x, "Style"] <- "Lame"
df[-x, "Style"] <- "Cool"
df$Style <- factor(df$Style)

p <- ggplot() + stat_summary(data = df, aes(Groups, Style, fill = Style),
geom = "bar", fun.y = length, position=position_dodge())

(Sorry, this is my first question... I don't know how to present code snippets like head(df) or the actual plot in SO. Please run this code to understand my question.)

So the plot adequately presents the count of every 'Style' per 'Groups'. However, the y axis scale shows the levels of the factor variable 'Style'. Although values I am plotting are originally discrete, the count of every 'Cool' and 'Lame' per 'Groups' is continuous.

How do I change the 'y' scale of my barplot from discrete to continuous in ggplot2, in order to correspond to the count values and not the original factor levels???

解决方案

You can take advantage of ggplot grouping and the histogram to do this for you

p <- ggplot(df, aes(Groups, fill=Style)) + geom_histogram(position=position_dodge())

这篇关于当使用fun.y ggplot时改变y的比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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