Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for type="h" in ggplot2

Tags:

r

ggplot2

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,data,geom="point")
like image 378
Francesca Avatar asked Sep 20 '14 16:09

Francesca


People also ask

What does %>% do in ggplot?

%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.

What does Geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot.

What does Geom_point mean?

geom_point.Rd. The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.


1 Answers

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

like image 79
Ben Bolker Avatar answered Oct 19 '22 16:10

Ben Bolker