【问题标题】:Color points by date in ggplot2ggplot2中按日期的颜色点
【发布时间】:2018-02-08 10:44:58
【问题描述】:

大家好:我正在努力在 ggplot2 中按日期着色点。这里有两个结果对我有用。 1) 通过变量 recent_elections 为点着色,并添加直线来表示每个点的最近选举日期。当前的代码就是这样做的。 2) 最好但更难,只需添加线条,每次选举颜色不同,显示打印最近联邦选举日期的图例。

我当前的数据和尝试如下。

library(dplyr)
library(tidyr)
library(ggplot2)

members <- structure(list(date = structure(c(6209, 6574, 7305, 14984, 15339, 
15341, 17169, 17174), class = "Date"), members = c(180835, 193225, 
200010, 86545, 95000, 128351, 41000, 124000), population = c(26449000, 
26798000, 27512000, 33476688, 33476688, 33476688, 35151728, 35151728
), votes_previous_election = c(2359915, 2685263, 2685263, 4508474, 
4508474, 4508474, 3470350, 3470350), vote_percent = c(18.8, 20.4, 
20.4, 30.6, 30.6, 30.6, 19.7, 19.7), seats_previous_election = c(32, 
43, 43, 103, 103, 103, 44, 44), recent_election = structure(c(5360, 
6899, 6899, 15096, 15096, 15096, 16727, 16727), class = "Date")), .Names = 
c("date", 
"members", "population", "votes_previous_election", "vote_percent", 
"seats_previous_election", "recent_election"), class = "data.frame", 
row.names = c(NA, 
-8L))

members %>% 
  select(population, votes_previous_election, seats_previous_election, members, 
         date, recent_election) %>% 
  mutate(., members_per_capita=members/population, 
         members_votes=members/votes_previous_election, 
         members_seats=members/seats_previous_election) %>% 
  gather(Variable, Value, c(members_per_capita,members_votes, 
                            members_seats))%>% 
  ggplot(., aes(x=date, y=Value, 
                group=recent_election))+
  geom_point(aes(fill=recent_election))+
  facet_wrap(~Variable, scales='free')+
  geom_vline(data=members, aes(xintercept=as.numeric(recent_election), col='red'), show.legend=F)

【问题讨论】:

    标签: r date ggplot2


    【解决方案1】:
    members %>% 
      select(population, votes_previous_election, seats_previous_election, members, 
             date, recent_election) %>% 
      mutate(., members_per_capita=members/population, 
             members_votes=members/votes_previous_election, 
             members_seats=members/seats_previous_election) %>% 
      gather(Variable, Value, c(members_per_capita,members_votes, 
                                members_seats))%>% 
      ggplot(., aes(x=date, y=Value, 
                    group=recent_election))+
      geom_point()+
      geom_vline(data=members, aes(xintercept=as.numeric(recent_election), col=factor(recent_election)), show.legend=T)+
      facet_wrap(~Variable, scales='free') +
      scale_color_discrete(name = "Recent Election") + xlim(as.Date("1984-01-01"), NA)
    

    我将geom_vline 中的col="red" 更改为col=factor(recent_election),以便垂直线由recent_election 着色。 factor() 确保 recent_election 被视为离散的而不是连续的。 scale_color_discrete 设置图例标题。请注意,选举日期“1984-09-04”超出了您点的 x 范围,因此我添加了 xlim(as.Date("1984-01-01"), NA) 以包括该选举日期。 NA 自动设置上限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-03
      • 2019-01-06
      • 2021-09-29
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 2020-08-24
      • 2020-11-27
      相关资源
      最近更新 更多