问题
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)
回答1:
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())

回答2:
I would use ggplot
instead of the qplot
command. You can reorder the bars inside your ggplot
function:
ggplot(result_Q1_data, aes(x=reorder(TYPE,-HARM), y=HARM, fill=reorder(TYPE,-HARM))) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle=45, vjust=0.5))
which gives:

来源:https://stackoverflow.com/questions/23858316/reordering-bars-in-qplot