簡體   English   中英

R中帶有ggplot的多行-圖例消失

[英]Multiple lines with ggplot in R - legend disappearing

我對R和ggplot相對較新,我試圖創建一個簡單的折線圖,其中包含多個隨時間變化的多條線。 我的數據框如下所示:

x <- c("2013-01", "2013-02", "2013-03", "2013-04", "2013-05", "2013-06", "2013-07", "2013-08", "2013-09", "2013-10", "2013-11", "2013-12",
       "2014-01", "2014-02", "2014-03", "2014-04", "2014-05", "2014-06", "2014-07", "2014-08", "2014-09", "2014-10", "2014-11", "2014-12",
       "2015-01", "2015-02", "2015-03", "2015-04", "2015-05", "2015-06", "2015-07", "2015-08", "2015-09", "2015-10", "2015-11", "2015-12",
       "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-06", "2016-07", "2016-08", "2016-09", "2016-10", "2016-11", "2016-12")

y.articles <- c(0, 1, 1, 2, 0, 0, 9, 6, 1, 0, 0, 1,
                3, 0, 0, 0, 4, 1, 7, 106, 19, 16, 57, 115,
                26, 20, 33, 52, 45, 36, 32, 23, 21, 38, 17, 18,   
                6, 10, 14, 6, 17, 5, 34, 6, 11, 11, 2, 2)

y.police <- c(0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 1, 29, 9, 2, 17, 46,
              13, 9, 14, 10, 13, 18, 6, 8, 7, 12, 1, 6,
              1, 2, 3, 1, 2, 2, 22, 2, 2, 7, 2, 1)

y.protester <- c(0, 0, 0, 2, 0, 0, 1, 2, 1, 0, 0, 1,
                 3, 0, 0, 0, 1, 0, 6, 51, 6, 11, 18, 38,
                 8, 9, 11, 32, 22, 10, 15, 10, 10, 19, 9, 5,
                 3, 6, 6, 2, 8, 3, 8, 3, 8, 4, 0, 1)

data <- data.frame(x, y.articles, y.police, y.protester)

我正在嘗試隨着時間(x)用三條線(y.articles,y.police和y.protester)制作線圖。 我還有兩條水平線標記事件。 這是我正在使用的代碼:

temp = ggplot(data=data, aes(x=x, y=y.articles, group=1)) +
  geom_line(aes(y = y.articles)) + 
  geom_line(aes(y = y.protester)) +
  geom_line(aes(y = y.police)) + 
  geom_line()+
  geom_point()+
  labs(title="Media Attention",x="", y = "Media Articles")+
  geom_vline(xintercept=19, linetype = "longdash") +
  geom_vline(xintercept=6, linetype = "longdash") +
  scale_linetype_manual(values = c(1,2,3)) +
  theme(axis.text.x = element_blank())

temp + theme(plot.title = element_text(hjust = 0.5, face="bold"), legend.position = "bottom", legend.title = element_blank())

我有幾個問題。 (1)圖例未顯示。 (2)我想對折線圖使用形狀(例如,一個帶圓圈的線一個,另一個帶正方形的線)。 我不是要繪制散點圖,而只是想更改線條的形狀,以使線條看起來不太相似。 (3)我取消了x軸刻度標記的標簽,但希望顯示一些日期(如果可能的話,即2013、2014、2015和2016)。

這是很多東西,但是任何指導都將不勝感激!

如果我對您的理解正確,那么tidyverse方法就可以解決問題。 我正在使用您的數據。

require(tidyverse)

 data %>% 
  gather(subject, value, -x) %>% 
  mutate(x = as.Date(paste(x,"-01",sep="")))  %>% 
  ggplot(aes(x=x, y=value,
             group=subject, color = subject, shape = subject)) +
  geom_line() +
  geom_point() +
  labs(title="Media Attention",x="", y = "Media Articles")+
  geom_vline(xintercept=19, linetype = "longdash") +
  geom_vline(xintercept=6, linetype = "longdash") +
  scale_linetype_manual(values = c(1,2,3)) +
  theme(plot.title = element_text(hjust = 0.5, face="bold"), 
        legend.position = "bottom", legend.title = element_blank())

情節:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM