Code for type=“h” in ggplot2

后端 未结 3 353
再見小時候
再見小時候 2021-01-18 08:29

This may sound really simple but I\'m trying to find the equivalent code to plot(x,y, type=\"h\") as a qplot code. I already have:

qplot(x,y,         


        
相关标签:
3条回答
  • 2021-01-18 08:48

    the answer of user12202013 is totally correct, but if you want to use qplot you can do it as follows:

    qplot(data = data, x = x, binwidth = 0.5)
    

    But, I think, if you need some help on R, you should go to Stackoverflow

    0 讨论(0)
  • 2021-01-18 08:52

    It's a little clunky, but I think you need geom_segment().

    d <- data.frame(x=1:5,y=c(0.1,0.4,0.8,0.2,0.9))
    library(ggplot2)
    qplot(x=x,xend=x,y=0,yend=y,data=d,geom="segment")
    ## or equivalently
    ggplot(d,aes(x=x,xend=x,y=0,yend=y))+geom_segment()
    

    This gives (y label adapted): Resulting plot with ggplot2

    In contrast, using the histogram with stat=identity:

    qplot(data = d, x=x, y=y, stat="identity")
    

    gives:

    enter image description here

    For completeness, the plot with type='h' looks like this:

    enter image description here

    0 讨论(0)
  • 2021-01-18 09:11

    With ggplot you only need a data frame with a vector of observations, not the count for each value.

    ggplot(data, aes(x = x)) + geom_histogram()
    
    0 讨论(0)
提交回复
热议问题