Color points by date in ggplot2

╄→гoц情女王★ 提交于 2021-01-28 08:07:08

问题


Hi all: I am struggling to color points by date in ggplot2. There are two outcomes that would work for me here. 1) colour the points by the variable recent_elections and just add straight lines denoting the date of the most recent election for each point. The current code does that. 2) preferably, but harder, just add the lines, coloured differently for each election, showing a legend that printed the date of the most recent federal election.

My current data and attempt is below.

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)


回答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)

I changed the col="red" in geom_vline to col=factor(recent_election) so that the vertical lines are colored by recent_election. The factor() makes sure that recent_election is treated as discrete instead of continuous. scale_color_discrete sets the legend title. Note that the election date "1984-09-04" is going out of the x range of your points, so I added a xlim(as.Date("1984-01-01"), NA) to also include that election date. NA sets the upper limit automatically.



来源:https://stackoverflow.com/questions/45965155/color-points-by-date-in-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!