在ggplot2中创建一个部分虚线 [英] Create a partial dashed line in ggplot2

查看:225
本文介绍了在ggplot2中创建一个部分虚线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中创建一个绘图,需要创建一个线,其中一些值是投影。投影以虚线表示。这是代码:

$ $ $ $ $ $ $ c $ df = data.frame(date = c(rep(2008:2013,by = 1)),
value = c(303,407,538,696,881,1094))


ggplot(df,aes(date,value,width = 0.64))+
geom_bar(stat =身份,fill =#336699,color =black)+
ylim(c(0,1400))+ opts(title =US Smartphone Users)+
opts(axis。 text.y = theme_text(family =sans,face =bold))+
opts(axis.text.x = theme_text(family =sans,face =bold))+
opts(plot.title = theme_text(size = 14,face =bold))+
xlab(Year)+ ylab(Users(in millions))+
opts( axis.title.x = theme_text(family =sans))+
opts(axis.title.y = theme_text(family =sans,angle = 90))+
geom_segment(aes x = 2007.6,xend = 2013,y = 550,yend = 1350),arrow = arrow(length = unit(0.4,cm)))

因此,我创建了一条从2008年延伸到2013年的线路。但是,我想要一条从2008年到2011年,并从2011年到最后一条虚线。我只是做了两个单独的线段,或者是有一个单独的命令,我可以使用它来获得所需的结果。

ggplot philosopy很简单。绘图中的每个元素都需要位于不同的图层上。因此,为了在不同的线型中得到两条线段,您需要两条 geom_segment 语句。

  ggplot(df)  [df $ date <= 2011,],aes(date,value,width = 0.64))+ 
geom_bar(stat =identity,fill =#336699,color =black)+
geom_bar(data = df [df $ date> 2011,],aes(date,value),
stat =identity,fill =#336699,color =black,alpha = 0.5) +
ylim(c(0,1400))+ opts(title =US Smartphone Users)+
opts(
axis.text.y = theme_text(family =sans, ),
axis.text.x = theme_text(family =sans,face =bold),
plot.title = theme_text(size = 14,face =bold ),
axis.title.x = theme_text(family =sans),
axis.title.y = theme_text(family =sans,angle = 90)
)+
xlab(Year)+ ylab(Users(in millions))+
geom_segment(aes(x = 2007.6,xend = 2011,y = 550,yend = 1050),linetype = 1)+
geom_segment(aes(x = 2011,xend = 2013,y = 1050,
arrow = arrow(length = unit(0.4,cm)),linetype = 2)


I'm creating a plot in R and need to create a line where some of the values are projections. The projections are represented as a dashed line. Here's the code:

df = data.frame(date=c(rep(2008:2013, by=1)),
                value=c(303,407,538,696,881,1094))


ggplot(df, aes(date, value, width=0.64)) + 
        geom_bar(stat = "identity", fill="#336699", colour="black") +
        ylim(c(0,1400)) + opts(title="U.S. Smartphone Users") +
        opts(axis.text.y=theme_text(family="sans", face="bold")) +
        opts(axis.text.x=theme_text(family="sans", face="bold")) +
        opts(plot.title = theme_text(size=14, face="bold")) +
        xlab("Year") + ylab("Users (in millions)") +        
        opts(axis.title.x=theme_text(family="sans")) +
        opts(axis.title.y=theme_text(family="sans", angle=90)) +
        geom_segment(aes(x=2007.6, xend=2013, y=550, yend=1350), arrow=arrow(length=unit(0.4,"cm")))

So I've created a line which extends from 2008 to 2013. However, I want a solid line from 2008 to 2011, and a dashed line from 2011 to the end. Do i just do two seperate line segments, or is there a seperate command I can use to get the desired result.

解决方案

The ggplot philosopy is simple. Each element of a plot needs to be on a different layer. Thus to get two line segments in different line types, you need two geom_segment statements.

I illustrate the same principle with geom_bar in different colours for your different periods.

ggplot(df[df$date<=2011, ], aes(date, value, width=0.64)) + 
    geom_bar(stat = "identity", fill="#336699", colour="black") +
    geom_bar(data=df[df$date>2011, ], aes(date, value),  
        stat = "identity", fill="#336699", colour="black", alpha=0.5) +
    ylim(c(0,1400)) + opts(title="U.S. Smartphone Users") +
    opts(
        axis.text.y=theme_text(family="sans", face="bold"), 
        axis.text.x=theme_text(family="sans", face="bold"), 
        plot.title = theme_text(size=14, face="bold"), 
        axis.title.x=theme_text(family="sans"), 
        axis.title.y=theme_text(family="sans", angle=90)
    ) +
    xlab("Year") + ylab("Users (in millions)") +        
    geom_segment(aes(x=2007.6, xend=2011, y=550, yend=1050), linetype=1) + 
    geom_segment(aes(x=2011, xend=2013, y=1050, yend=1350), 
        arrow=arrow(length=unit(0.4,"cm")), linetype=2) 

这篇关于在ggplot2中创建一个部分虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆