重新排序qplot中的小节 [英] reordering bars in qplot

查看:131
本文介绍了重新排序qplot中的小节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据:

 > result_Q1_data 
HARM TYPE
834 96979 TORNADO
130 8428过热$ b $ 856 7461 TSTM WIND
170 7259 FLOOD
464 6046闪电
275 3037 HEAT
153 2755 FLASH FLOOD
427 2064 ICE STORM
760 1621 THUNDERSTORM WIND
972 1527冬季风暴

我想制作一个条形图。我使用这个命令:

  qplot(TYPE,HARM,data = result_Q1_data,stat =identity,geom =bar ,fill = EVTYPE)

并得到 plot

数据会在图上减少,x轴上不会有文字,或者它们会是垂直的? (我对英语还不擅长,对不好的解释感到抱歉)

解决方案

我推荐你使用ggplot函数,但是如果你想使用qplot。您需要重新排列factor TYPE的级别并删除x轴的标签。

  result_Q1_data = data.frame(
'id'= c(834,130,856,170,464,275,153,427,760,972),
'HARM'= c(96979,8428,7461,7259,6046,3037,
'TYPE'=因子(c('TORNADO','EXCESSIVE HEAT','TSTM WIND','FLOOD','LIGHTNING',
''HEAT' ,'FLASH FLOOD','ICE STORM','THUNDERSTORM WIND',
'WINTER STORM')))

result_Q1_data $ TYPE =因子(result_Q1_data $ TYPE,
levels = result_Q1_data $ TYPE [order(result_Q1_data $ HARM,decrease = T)])

qplot(TYPE,HARM,data = result_Q1_data,stat =identity,geom =bar,fill = TYPE )+
主题(axis.ticks = element_blank(),axis.text.x = element_blank())


I have this data:

> result_Q1_data
     HARM              TYPE
834 96979           TORNADO
130  8428    EXCESSIVE HEAT
856  7461         TSTM WIND
170  7259             FLOOD
464  6046         LIGHTNING
275  3037              HEAT
153  2755       FLASH FLOOD
427  2064         ICE STORM
760  1621 THUNDERSTORM WIND
972  1527      WINTER STORM

And I want to make a bar plot. I am using this command:

qplot(TYPE,HARM,data=result_Q1_data,stat="identity",geom="bar", fill=EVTYPE)

and get this plot

How can I make the same plot where the data would decrease on the plot and there would not be words on x axis or they would be vertical? (I'm not good at english yet, sorry for bad explanation)

解决方案

I recommend you to use ggplot function, but if you want to use qplot. You need to reorder the levels of factor TYPE and remove the labels of x axis.

result_Q1_data = data.frame(
    'id' = c(834, 130, 856, 170, 464, 275, 153, 427, 760, 972),
    'HARM' = c(96979, 8428, 7461, 7259, 6046, 3037, 2755, 2064, 1621, 1527),
    'TYPE' = factor(c('TORNADO', 'EXCESSIVE HEAT', 'TSTM WIND', 'FLOOD', 'LIGHTNING',
       'HEAT', 'FLASH FLOOD', 'ICE STORM', 'THUNDERSTORM WIND', 
       'WINTER STORM')))

result_Q1_data$TYPE = factor(result_Q1_data$TYPE, 
         levels = result_Q1_data$TYPE[order(result_Q1_data$HARM, decreasing=T)])

qplot(TYPE,HARM,data=result_Q1_data,stat="identity",geom="bar", fill=TYPE) + 
    theme(axis.ticks = element_blank(), axis.text.x = element_blank())

这篇关于重新排序qplot中的小节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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