漂浮的barcharts [英] Floating barcharts

查看:110
本文介绍了漂浮的barcharts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作可以指定最小值的条形图(很像盒子和晶须图中的框)。 Barplot可以做到吗?我怀疑ggplot的答案,但我找不到一个例子。
以下是一些数据:


  X一月二月三月四月五月六月七月八月九月十月11月12月
1最高记录31.5 31.8 30.3 28.0 24.9 24.4 21.7 20.9 24.5 25.4 26.0 28.7
2月平均最高值27.8 28.6 27.0 24.8 22.0 20.0 18.9 18.8 20.4 22.4 23.9 26.8
3平均每日最高值24.2 24.8 23.1 20.9 18.4 16.3 15.5 15.7 16.9 18.3 20.0 22.4
4平均19.1 19.8 18.1 16.2 13.8 11.9 11.2 11.6 12.7 14.1 15.7 17.7
5平均每日最低14.0 14.7 13.1 11.4 9.2 7.5 6.9 7.4 8.4 10.0 11.4 13.0
6平均每月最低7.6 9.1 6.8 3.8 2.3 -0.5 -0.2 1.0 2.3 3.7 5.3 6.7
7最低记录4.0 5.6 4.1 -1.3 0.0 -3.1 -2.6 -1.4 -0.8 2.0 2.7 4.1



xaxis = c(J,F,M,A,M,J A,S,O,N,D)
<理想情况下,我最终每个月都会有一个堆叠酒吧,从最低开始记录'的价值,而不是零。



我也尝试了使用UsingR软件包的superbarplot。我可以让条形图开始我想要的位置,但不能将x轴向下移出绘图中心。您可以在<$ c中使用 geom_boxplot > $ c> ggplot2 得到你想要的(我认为)指定预先计算的值和 stat ='identity'并使用 geom_crossbar 放入其他

 #首先,您的数据
天气< - read.table(text ='X一月二月三月四月五月六月七月八月九月十月十一月十二月
1最高记录31.5 31.8 30.3 28.0 24.9 24.4 21.7 20.9 24.5 25.4 26.0 28.7
2平均每月最高27.8 28.6 27.0 24.8 22.0 20.0 18.9 18.8 20.4 22.4 23.9 26.8
3平均每日最高限额24.2 24.8 23.1 20.9 18.4 16.3 15.5 15.7 16.9 18.3 20.0 22.4
4平均数19.1 19.8 18.1 16.2 13.8 11.9 11.2 11.6 12.7 14.1 15.7 17.7
5平均每日最低限额14.0 14.7 13.1 11.4 9.2 7.5 6.9 7.4 8.4 10.0 11.4 13.0
6平均每月最低限额7.6 9.1 6.8 3.8 2.3 -0.5 -0.2 1.0 2.3 3.7 5.3 6.7
7L owest记录4.0 5.6 4.1 -1.3 0.0 -3.1 -2.6 -1.4 -0.8 2.0 2.7 4.1',header = T)


library(reshape2)
library(ggplot2)
#重塑为宽格式(基本上转置data.frame)
w < - dcast(melt(weather),variable〜X)
ggplot(w,aes(x = variable,ymin =`最低记录数`,
ymax =`最高记录数`,较低=`最低记录数`,
upper =`最高记录数`,middle =`平均每日最大数`))+
geom_boxplot ('stat'='identity')+
xlab('month')+
ylab('Temperature')+
geom_crossbar(aes(y =`平均每月最大值))+
geom_crossbar(aes(y =`Mean average minimum``))+
geom_crossbar(aes(y =`平均每日最大值))+
geom_crossbar(aes(y =`平均每日最小值`) )


这部分在帮助中的示例中进行了描述为 geom_boxplot


I want to make bar charts where the bar minimum can be specified (much like the box in a box and whisker plot). Can barplot do that? I suspect the answer's in ggplot, but I can't find an example. Here's some data:

                    X  Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1      Highest recorded 31.5 31.8 30.3 28.0 24.9 24.4 21.7 20.9 24.5 25.4 26.0 28.7
2  Mean monthly maximum 27.8 28.6 27.0 24.8 22.0 20.0 18.9 18.8 20.4 22.4 23.9 26.8
3    Mean daily maximum 24.2 24.8 23.1 20.9 18.4 16.3 15.5 15.7 16.9 18.3 20.0 22.4
4                  Mean 19.1 19.8 18.1 16.2 13.8 11.9 11.2 11.6 12.7 14.1 15.7 17.7
5    Mean daily minimum 14.0 14.7 13.1 11.4  9.2  7.5  6.9  7.4  8.4 10.0 11.4 13.0
6  Mean monthly minimum  7.6  9.1  6.8  3.8  2.3 -0.5 -0.2  1.0  2.3  3.7  5.3  6.7
7       Lowest recorded  4.0  5.6  4.1 -1.3  0.0 -3.1 -2.6 -1.4 -0.8  2.0  2.7  4.1

xaxis =c("J" ,"F" ,"M" ,"A" ,"M" ,"J","J","A", "S", "O","N","D")

So ideally, I end up with a stacked bar for each month, that starts at the 'Lowest recorded' value, rather than at zero.

I've also had a try with superbarplot from the UsingR package. I can get the bars to start where I want, but can't move the x axis down out of the centre of the plot. Thanks in advance.

解决方案

You can use geom_boxplot in ggplot2 to get what (I think) you want specifying the precomputed values and stat = 'identity' and use geom_crossbar to put in the other

# first, your data
weather <- read.table(text = 'X  Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1      "Highest recorded" 31.5 31.8 30.3 28.0 24.9 24.4 21.7 20.9 24.5 25.4 26.0 28.7
2  "Mean monthly maximum" 27.8 28.6 27.0 24.8 22.0 20.0 18.9 18.8 20.4 22.4 23.9 26.8
3    "Mean daily maximum" 24.2 24.8 23.1 20.9 18.4 16.3 15.5 15.7 16.9 18.3 20.0 22.4
4                  "Mean" 19.1 19.8 18.1 16.2 13.8 11.9 11.2 11.6 12.7 14.1 15.7 17.7
5   "Mean daily minimum" 14.0 14.7 13.1 11.4  9.2  7.5  6.9  7.4  8.4 10.0 11.4 13.0
6  "Mean monthly minimum"  7.6  9.1  6.8  3.8  2.3 -0.5 -0.2  1.0  2.3  3.7  5.3  6.7
7       "Lowest recorded"  4.0  5.6  4.1 -1.3  0.0 -3.1 -2.6 -1.4 -0.8  2.0  2.7  4.1', header =T)


library(reshape2)
library(ggplot2)
# reshape to wide format (basically transposing the data.frame)
w <- dcast(melt(weather), variable~X)
ggplot(w, aes(x=variable,ymin = `Lowest recorded`, 
         ymax = `Highest recorded`, lower = `Lowest recorded`, 
         upper = `Highest recorded`, middle = `Mean daily maximum`)) + 
     geom_boxplot(stat = 'identity') +
     xlab('month') + 
     ylab('Temperature') +
     geom_crossbar(aes(y = `Mean monthly maximum` ))+
     geom_crossbar(aes(y = `Mean monthly minimum`)) +
     geom_crossbar(aes(y = `Mean daily maximum` ))+
     geom_crossbar(aes(y = `Mean daily minimum`)) 

This is partially described in an example in the help for geom_boxplot

这篇关于漂浮的barcharts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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