用ggplot在同一直方图中绘制两个变量 [英] Plot two variables in the same histogram with ggplot

查看:151
本文介绍了用ggplot在同一直方图中绘制两个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,我想通过使用ggplot制作一个直方图.我想在x轴和y轴上包含森林,农业,建筑,其他和水的情况,这是每种土地用途的入库误差和遗漏误差.有人可以帮我吗.谢谢

I have a dataframe and I want to make a histogram out of it by using ggplot. I want to have Forest, Agriculture, Built-up, Other and Water in the x-axis and in the y axis would be both comission and omission errors for each land use. Can someone help me out. Thanks

 dput(n)
    structure(c(90.49964814, 36.77437804, 18.61958266, 57.14285714, 
    20, 100, 9.53346856, 45.63106796, 0, 0), .Dim = c(2L, 5L), .Dimnames = list(
        c("Omission Error", "Comission Error"), c("Forest", "Agriculture", 
        "Built-up", "Other", "Water")))

推荐答案

或者该替代方法适合吗?

Or would this alternative fit?

library(reshape2); library(ggplot2)
df2 <- melt(df, id.var=Forest)
#                 X1          X2      value
# 1   Omission Error      Forest  90.499648
# 2  Comission Error      Forest  36.774378
# 3   Omission Error Agriculture  18.619583
# 4  Comission Error Agriculture  57.142857
# 5   Omission Error    Built-up  20.000000
# 6  Comission Error    Built-up 100.000000
# 7   Omission Error       Other   9.533469
# 8  Comission Error       Other  45.631068
# 9   Omission Error       Water   0.000000
# 10 Comission Error       Water   0.000000

ggplot(df2, aes(x=X2, y=value)) + geom_bar(stat="identity") + facet_grid(X1~.)

这篇关于用ggplot在同一直方图中绘制两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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