在ggplot中的barplot [英] barplot in ggplot

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

问题描述

我在使用ggplot制作barplot时遇到问题。
我尝试了qplot和gplot的不同组合,
,但我要么得到一个直方图,要么换掉我的酒吧,或者决定使用logscaling。



使用普通的绘图功能。
我会这样做:

  d <-1 /(10:1)
名称(d )< -paste(id,1:10)
barplot(d)

感谢

解决方案

要在ggplot2中绘制条形图,您必须使用 geom =bar geom_bar 。您是否尝试过任何 ggplot2网站上的geom_bar示例



为了让你的例子工作,请尝试以下方法:


  • ggplot 需要一个data.frame作为输入。因此,将输入数据转换为data.frame。
  • 使用`aes(x = x,y = y)将数据映射到图上的美观性。这告诉ggplot数据中哪些列映射到图表上的哪些元素。

  • 使用 geom_plot 创建条形图。在这种情况下,您可能想要告诉 ggplot 数据已经使用 stat =identity进行汇总,因为(请注意,函数 barplot )可以创建一个直方图。



  • 您在示例中使用的是基本R图形的一部分,而不是 ggplot 。)



    (x = 1:10,y = 1 /(10:1))


      d < -  data.frame ggplot(d,aes(x,y))+ geom_bar(stat =identity)

    < img src =https://i.stack.imgur.com/lCOdt.pngalt =在这里输入图片描述>


    I'm having problems making a barplot using ggplot. I tried different combinations of qplot and gplot, but I either get a histogram, or it swaps my bars or it decides to use logscaling.

    Using the ordinary plot functions. I would do it like

    d<-1/(10:1)
    names(d) <-paste("id",1:10)
    barplot(d)
    

    Thanks

    解决方案

    To plot a bar chart in ggplot2, you have to use geom="bar" or geom_bar. Have you tried any of the geom_bar example on the ggplot2 website?

    To get your example to work, try the following:

    • ggplot needs a data.frame as input. So convert your input data into a data.frame.
    • map your data to aesthetics on the plot using `aes(x=x, y=y). This tells ggplot which columns in the data to map to which elements on the chart.
    • Use geom_plot to create the bar chart. In this case, you probably want to tell ggplot that the data is already summarised using stat="identity", since the default is to create a histogram.

    (Note that the function barplot that you used in your example is part of base R graphics, not ggplot.)

    The code:

    d <- data.frame(x=1:10, y=1/(10:1))
    ggplot(d, aes(x, y)) + geom_bar(stat="identity")
    

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

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