Code for type = "h" in ggplot2

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

qplot(x,y,data,geom="point")
+4
source share
3 answers

This is a little awkward, but I think you need to 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 (adaptable label y): Resulting plot with ggplot2

In contrast, using a histogram with stat=identity:

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

gives:

enter image description here

For completeness, plotc is type='h'as follows:

enter image description here

+4
source

user12202013's answer is absolutely correct, but if you want to use qplot, you can do it like this:

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

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

+1

ggplot , .

ggplot(data, aes(x = x)) + geom_histogram()
0

All Articles