R 从零开始绘制图例
我应该能做到,但我不知所措。鉴于下面的(丑陋的)情节,我如何制作一个带有三行标记为“CI 90”、“CI 95”、“CI 99”且颜色为R 从零开始绘制图例,r,ggplot2,R,Ggplot2,我应该能做到,但我不知所措。鉴于下面的(丑陋的)情节,我如何制作一个带有三行标记为“CI 90”、“CI 95”、“CI 99”且颜色为my.cols[3:1]的图例。我不想改变数据的形状(foo)。非常感谢 library(tidyverse) foo <- tibble(name = letters[1:2], ci90lwr = c(0.8, 0.9), ci90upr = c(1.2, 1.3),
my.cols[3:1]
的图例。我不想改变数据的形状(foo
)。非常感谢
library(tidyverse)
foo <- tibble(name = letters[1:2],
ci90lwr = c(0.8, 0.9),
ci90upr = c(1.2, 1.3),
ci95lwr = c(0.7, 0.8),
ci95upr = c(1.3, 1.4),
ci99lwr = c(0.6, 0.7),
ci99upr = c(1.4, 1.5))
my.cols <- rainbow(3)
foo %>% ggplot() +
geom_segment(mapping = aes(x= name,
xend = name,
y= ci90lwr,
yend = ci90upr),
size=5,col=my.cols[3]) +
geom_segment(mapping = aes(x= name,
xend = name,
y= ci95lwr,
yend = ci95upr),
size=3,col=my.cols[2]) +
geom_segment(mapping = aes(x= name,
xend = name,
y= ci99lwr,
yend = ci99upr),
size=1,col=my.cols[1]) +
labs(y="Estimate",x="Group") +
theme_minimal()
库(tidyverse)
foo为my.cols
中的每个色调值指定一个名称,以便ggplot可以将美学“颜色”中的值映射到my.cols
中的色调
names(my.cols) = my.cols
要获得颜色的图例,您需要在每个geom_段()
中将颜色作为美学元素
要进行自定义映射(即不是默认的ggplot颜色),请使用scale\u color\u manual()。您还可以自定义标签和自定义标题。Inside aes()当然可以。谢谢。
foo %>% ggplot() +
geom_segment(mapping = aes(x= name,
xend = name,
y= ci90lwr,
yend = ci90upr,
colour = my.cols[3]),
size=5) +
geom_segment(mapping = aes(x= name,
xend = name,
y= ci95lwr,
yend = ci95upr,
colour = my.cols[2]),
size=3) +
geom_segment(mapping = aes(x= name,
xend = name,
y= ci99lwr,
yend = ci99upr,
colour = my.cols[1]),
size=1) +
labs(y="Estimate",x="Group") +
theme_minimal() +
scale_colour_manual(values = my.cols,labels = c("CI 99", "CI 95", "CI 90")) +
labs(colour = "Colour Variable")