简体   繁体   中英

How to plot graph type='h' in ggplot2

I'm wondering how to plot in ggplot2 something like this: let's say I've got two numeric vectors: time <-c(1,3,4,6,9,10,12), n.censor<-c(0,0,1,4,0,3,1) and I'd like to plot: plot(n.censor~time,type='h')

How to achieve something like this in ggplot2 ?

In this case, a "histogram" is the look you want, but the data are coded as if they're for a bar graph, since they are already aggregated. As such, your stat will be "identity". Here's some code to use for ggplot() :

 # first put your vectors into a data.frame
 df <- data.frame(time, n.censor)
 # plot
 ggplot(df, aes(x=time, y=n.censor))+
    geom_bar(stat="identity")

 # or alternatively, with the histogram layer:
 ggplot(df, aes(x=time, y=n.censor))+
    geom_histogram(stat="identity")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM