ggplot具有连续比例的boxplot多因素级分组 [英] ggplot multi-factor-level grouping for boxplot with continuous scale

查看:243
本文介绍了ggplot具有连续比例的boxplot多因素级分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Temp< -rnorm(90,mean = 100, sd = 10)
Yr <-sample(c(1999,2000,2005,2009,2010),size = 90,replace = TRUE)
Month< -sample(c(June,July,August),size = 90,replace = TRUE)
Month
df< -data.frame(Temp,Month,Yr)

我想要的视觉效果和相应的代码如下:

  ggplot(df,aes(x = interaction(Month ,yr)y = Temp,fill = Month))+ 
geom_boxplot()+
xlab(Year)+
ylab(每日最高温度)

但是,您会注意到数据中缺少几年,我试图使情节反映了x尺度的差距。另一个问题是轴上的文本和刻度线。我希望标记只是观察年份而不是月份。因为本月已经在填充中编码。我试过scale_x_discrete,但试图为连续轴提供离散值时会吐出一个空白图形和一个错误。我已经在当天的计算机配额中遇见了我,并且在这方面得到一些帮助真的很棒。

这造成了巨大的差距,因为每年都会有自己的差距,但是您可以通过仅将特定年份作为factor()调用的参数传递来适应这种情况。



< (df,aes(x = Yr,y = Temp),其中, ,fill = Month))+
geom_boxplot(position = position_dodge(1))+
ylab(每日最高温度)+
scale_x_discrete(Year,drop = FALSE)


I'm trying to create a boxplot of the following data

Temp<-rnorm(90,mean=100,sd=10)
Yr<-sample(c("1999","2000","2005","2009","2010"),size=90,replace=TRUE)
Month<-sample(c("June","July","August"),size=90,replace=TRUE)
Month
df<-data.frame(Temp,Month,Yr)

The visual I want and its corresponding code are below:

ggplot(df,aes(x=interaction(Month,Yr),y=Temp,fill=Month))+
    geom_boxplot()+
    xlab("Year")+
    ylab("Daily Maximum Temperature")

You'll notice, though, that there are a few years missing from the data, and I'm trying to make the plot reflect that with gaps in the x-scale. The other problem is the text and tick marks on the axis. I'd like the ticks to just be the Year of observation rather than Month.Year since the month is already coded in the fill. I've tried scale_x_discrete, but trying to supply discrete values for a continuous axis spits out a blank graph and an error. I've met my swearing at the computer quota for the day, and it would be really awesome to get a little help on this.

解决方案

This creates huge gaps, as every year gets its own gap, but you can adapt this by passing only specific years as the levels argument to the factor() call.

df$Yr <- factor(df$Yr, levels=1999:2010)

ggplot(df,aes(x=Yr,y=Temp,fill=Month))+
  geom_boxplot(position=position_dodge(1))+
  ylab("Daily Maximum Temperature") +
  scale_x_discrete("Year", drop=FALSE)

这篇关于ggplot具有连续比例的boxplot多因素级分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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