问题描述
大家好:我正在努力按ggplot2中的日期对点进行着色.这里有两个对我有用的结果.1)使用变量last_elections为这些点着色,并仅添加表示每个点最近一次选举的日期的直线.当前代码可以做到这一点.2)最好但更难的是,只需添加线条,每次选举的颜色都不同,以显示印有最近联邦选举日期的图例.
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)
推荐答案
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
自动设置上限.
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.
这篇关于在ggplot2中按日期颜色点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!