Vertical box-percentile plot with Lattice & panel.bpplot

自作多情 提交于 2019-12-07 19:09:12

问题


I am drawing box-percentile plots in R, using the box-percentile panel function from Hmisc (panel.bpplot) with bwplot from lattice.

I have a numeric vector (Length), and would like to show its distribution across the levels of a factor variable (Month).

Here's an example with fake data:

For example,

set.seed(13)
Length<-sample(1:10, 1000, replace=TRUE)
Month<-sample(c("Apr","May","Jul","Aug","Sep","Nov"), 1000, replace=TRUE)

df<-cbind(Month, Length)
df<-as.data.frame(df)   
df$Month<-factor(df$Month, levels=c("Apr","May","Jul","Aug","Sep","Nov"))
df$Length<-as.numeric(df$Length)

#plot horizontal box-percentile plot; 
bwplot(Month~Length, data=df, panel=panel.bpplot)

This works fine. But I want the plots to be vertical, with Month on the x-axis and Length on the y-axis. The documentation for panel.bpplot says that horizontal plots make category levels more visible, but, for my purposes, I specifically need a vertical plot. Is there a way to modify panel.bpplot to do this?


回答1:


I took some tips from this previous question and then merged it with your code. It seems the "trick" is setting up a rotated grid:

require(grid)
grid.newpage()
pushViewport(viewport(angle = 90, name = "VP"))
print(
    bwplot(Month~Length, data=df, panel=panel.bpplot, draw.in = "VP"
    ),
    newpage=FALSE
)

which results in:



来源:https://stackoverflow.com/questions/8053360/vertical-box-percentile-plot-with-lattice-panel-bpplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!