按最后一个值r绘制颜色线
我需要在ggplot中绘制>741行的帮助按最后一个值r绘制颜色线,r,ggplot2,colors,line,R,Ggplot2,Colors,Line,我需要在ggplot中绘制>741行的帮助 一条特定线的颜色不应改变,例如,颜色线应仅由eci的最终值指定 我希望在每行的开头和结尾显示每行的名称(在代码示例“unit”中) 当然,700多条线很难用肉眼分辨,但有什么建议可以让这些线更容易分辨吗 df <- data.frame(unit=rep(1:741, 4), year=rep(c(2012, 2013, 2014, 2015), each=741), eci
df <- data.frame(unit=rep(1:741, 4),
year=rep(c(2012, 2013, 2014, 2015), each=741),
eci=round(runif(2964, 1, 741), digits = 0))
g = ggplot(data = df, aes(x=year, y=eci, group=unit)) +
geom_line(aes(colour=eci), size=0.01) +
scale_colour_gradientn(colours = terrain.colors(10)) +
geom_point(aes(colour=eci), size=0.04)
# The colour of the line should be determined by all eci for which year=2015
df实现所需结果的一种方法是创建带有额外信息的新列,以便在使用ggplot2
进行打印时使用
使用dplyr
,我们将数据按单位分组,然后对其进行排列,这样我们就可以创建一列来存储上一个eci的值,以及两列带有第一年和去年标签的数据,这样我们就可以将它们作为文本添加到绘图中
df_new <- df %>%
group_by(unit) %>%
arrange(unit, year, eci) %>%
mutate(last_eci = last(eci),
first_year = ifelse(year == 2012, unit, ""),
last_year = ifelse(year == 2015, unit, ""))
当然,查看生成的绘图很容易看出,尝试在单个绘图中绘制>700行不同颜色和>1400个标签不是非常可取的
我会使用df
的相关子集,因此我们会生成有助于更好地理解数据的图
df_new %>%
filter(unit %in% c(1:10)) %>%
ggplot(data = .,
aes(x = year, y = eci, group = unit, colour = last_eci)) +
geom_line(size = 0.01) +
geom_text(aes(label = first_year), nudge_x = -0.05, color = "black") +
geom_text(aes(label = last_year), nudge_x = 0.05, color = "black") +
scale_colour_gradientn(colours = terrain.colors(10)) +
geom_point(aes(colour = eci), size = 0.04)
为了更好的可读性,我选择了使用directlabels包的10行示例
library(ggplot2)
library(dplyr)
library(directlabels)
set.seed(95)
l <- 10
df1 <- data.frame(unit=rep(1:l, 4),
year=rep(c(2012, 2013, 2014, 2015), each=l),
eci=round(runif(4*l, 1, l), digits = 0))
df2 <- df1 %>% filter (year == 2015) %>% select(-year, end = eci)
df <- left_join(df1,df2, by = "unit")
g <-
ggplot(data = df, aes(x=year,
y=eci,
group=unit)) +
geom_line(aes(colour=end), size=0.01) +
scale_colour_gradientn(colours = terrain.colors(10)) +
geom_point(aes(colour=eci), size=0.04) +
geom_dl(aes(label = unit,color = end), method = list(dl.combine("first.points", "last.points"), cex = 0.8))
g
库(ggplot2)
图书馆(dplyr)
库(directlabels)
种子集(95)
l半年后,我认为基于parcoord()
应用于广泛df的解决方案更简单
set.seed(95)
l <- 1000 # really 1000 observations per year this time
df1 <- data.frame(unit=rep(1:l, 4),
year=rep(c(2012, 2013, 2014, 2015), each=l),
eci=round(runif(4*l, 1, l), digits = 0))
df1 <- tidyr::spread(df1, year, eci) # change from long to wide
df1 <- df1 %>%
dplyr::arrange(desc(`2015`)) # Assign after which column (year) rows should be ordered
# create 10 different colrs which are repeated 100 times
my_colors=rep(terrain.colors(11)[-1], each=100)
parcoord(df1[, c(2:5)] , col= my_colors)
set.seed(95)
我很抱歉-不知何故,我现在无法将代码格式化为正确的格式。出色的1行解决方案!不幸的是,geom_dl在处理大于200个值的数据时非常慢。
set.seed(95)
l <- 1000 # really 1000 observations per year this time
df1 <- data.frame(unit=rep(1:l, 4),
year=rep(c(2012, 2013, 2014, 2015), each=l),
eci=round(runif(4*l, 1, l), digits = 0))
df1 <- tidyr::spread(df1, year, eci) # change from long to wide
df1 <- df1 %>%
dplyr::arrange(desc(`2015`)) # Assign after which column (year) rows should be ordered
# create 10 different colrs which are repeated 100 times
my_colors=rep(terrain.colors(11)[-1], each=100)
parcoord(df1[, c(2:5)] , col= my_colors)