【问题标题】:How to plot a line graph with discontinuity data?如何绘制具有不连续数据的折线图?
【发布时间】:2014-03-06 17:08:16
【问题描述】:

我在使用 R 中的 ggplot2 库绘制图形时遇到问题。 我有一个“日期”变量,其中包含 3 个“品牌”品牌中每一个的披露值“评级”的日期。问题是,一场赛事过后,市场只有两个品牌共享。所以,我想在同一个情节中分别为这个事件之前和之后绘制线条。可能吗?如果我使用平滑函数按原样绘制所有内容,则图表毫无意义,所以我必须在两个阶段之间留下一个迄今为止,在下面的日期是“16/09/2012”和“18/09/2012”之间或在第五和第六元素之间。

x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
as.Date(x, "%d%b%Y")
x <- c("10/09/2012", "12/09/2012", "11/09/2012")
as.Date(x, "%d/%m/%Y")

x <- c("12/09/2012", "13/09/2012", "14/09/2012", "15/09/2012","16/09/2012","18/09/2012","19/09/2012","20/09/2012","22/09/2012","23/09/2012")
x <- as.Date(x, "%d/%m/%Y")
y <-c(30,32,33,34,35,45,46,44,46,47)
y2<-c(20,22,23,27,29,55,54,56,54,53)
y3 <- c(10,10,10,7,6,NA,NA,NA,NA,NA)
data1 <- data.frame(x,y1)
data1$w <-"A"
data2 <- data.frame(x,y2)
data2$w <-"B"
data3 <- data.frame(x,y3)
data3$w <-"C"

colnames(data1)<- c("Date", "Rating", "Brand")
colnames(data2)<- c("Date", "Rating", "Brand")
colnames(data3)<- c("Date", "Rating", "Brand")

data <-rbind(data1,data2,data3)

#Chart
(plot <-ggplot(data, aes(x=Date, 
y=Rating, colour=Brand, group=Brand)) 
+ geom_line(size=.5)
+ geom_smooth(aes(x=Date, y=Rating), 
method="loess", size=3, se=F) )

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    作为替代方法,并使用@jlhoward 的Event 变量: 使用BrandEvent 之间的交互作为分组变量,然后在一个面板中绘制,将不连续性显示为阴影区域。

    data$Event <- "Before"
    data[data$Date>="2012-09-18",]$Event <- "After"
    data$Event <- factor(data$Event, levels=c("Before","After"))
    
    xmin <- as.Date("16/09/2012", "%d/%m/%Y") # Beginning and end of discontinuity
    xmax <- as.Date("18/09/2012", "%d/%m/%Y")
    ggplot(data) +
      geom_line(aes(x = Date, y = Rating, colour = Brand, group=interaction(Brand, Event)), size = 1) +
      geom_rect(aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf), fill = "salmon", alpha = .01)
    

    编辑 现在添加geom_smooth

    ggplot(data) +
      geom_line(aes(x = Date, y = Rating, colour = Brand, group = interaction(Brand, Event)), size = .5) +
      geom_smooth(aes(x=Date, y=Rating, colour = Brand, group = interaction(Brand, Event)), method="loess", size=1, se=F) +
      geom_rect(aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf), fill = "salmon", alpha = .01)
    

    【讨论】:

    • 我虽然是这样,但你如何处理geom_smooth(...)
    • 看来geom_smooth 懂得分组美学。请参阅我的答案的编辑。在geom_smooth 中尝试不使用分组美学。
    【解决方案2】:

    这样的?

    data$Event <- "Before"
    data[data$Date>="2012-09-18",]$Event <- "After"
    data$Event <- factor(data$Event, levels=c("Before","After"))
    #Chart
    ggplot(data,aes(x=Date, y=Rating, colour=Brand, group=Brand))+
     geom_line(size=.5) +
     geom_smooth(aes(x=Date, y=Rating), method="loess", size=1, se=F)+ 
       facet_wrap(~Event, scales="free_x")
    

    这会添加一个新列data$Event,它在事件之前采用值“Before”,在事件之后采用“After”值。然后我们在 ggplot 中使用 facets 来分隔 Before 和 After。

    原因:

    data$Event <- factor(data$Event, levels=c("Before","After"))
    

    是创建一个有序因子:ggplot 想要按字母顺序(之后,然后之前)绘制构面,除非我们这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 2017-11-30
      • 2023-03-12
      相关资源
      最近更新 更多