简体   繁体   English

如何在ggplot2中绘制图形类型='h'

[英]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') 我想知道如何在ggplot2中绘制类似这样的图形:假设我有两个数字矢量: time <-c(1,3,4,6,9,10,12), n.censor<-c(0,0,1,4,0,3,1)我想作图: plot(n.censor~time,type='h')

How to achieve something like this in ggplot2 ? 如何在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() : 这是用于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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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