Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot修改图例以具有自定义元素_R_Ggplot2 - Fatal编程技术网

R ggplot修改图例以具有自定义元素

R ggplot修改图例以具有自定义元素,r,ggplot2,R,Ggplot2,我有以下代码: library(ggplot2) library(RColorBrewer) colours <-brewer.pal(n = 3, name = 'Paired') ids <- c("TestA", "TestB", "TestC") bg <-c(23, 13, 15) s1 <- c(21,15,17) s2 <- c(27,25,11) s3 <- c(24,14,

我有以下代码:

 library(ggplot2)
 library(RColorBrewer)


colours <-brewer.pal(n = 3, name = 'Paired')

ids <- c("TestA", "TestB", "TestC")
bg <-c(23, 13, 15)
s1 <- c(21,15,17)
s2 <- c(27,25,11)
s3 <- c(24,14,18)
df <- data.frame(ids, bg, s1,s2,s3)
colors <- c("bgs"= "grey", "tid1"=colours[1], "tid2"=colours[2], "tid3"=colours[3])


ggplot(df, aes(x = ids)) + 
  geom_col(aes(y = bg, color = 'bgs'), fill = 'grey', size = 1, ) +
  geom_point(aes(y = s1, color= 'tid1'), size = 10, group = 1) +
  geom_point(aes(y = s2, color= 'tid2'), size = 10, group = 1) +
  geom_point(aes(y = s3, color='tid3'), size = 10, group = 1)+ 
  labs(x = "Year",
       y = "(%)",
       color = "Legend") +
  scale_color_manual(values = colors)+
  theme(legend.position="bottom")
库(ggplot2)
图书馆(RColorBrewer)

如果我错了,请纠正我,但我认为您只是想删除图例中没有显示任何内容的图形框?我会在评论中询问,但我还没有足够的声誉:(

发生这种情况是因为您在
aes()
参数中指定了
color=
。最简单的方法是去掉
color=bgs

由于您使用的是条形图,
color=
所做的是在图形周围添加指定颜色的“边框”。而
fill=
是实际填充条形图颜色的颜色。下面是代码和图形的外观:

库(ggplot2)
#>警告:包“ggplot2”是在R版本3.6.3下生成的
图书馆(RColorBrewer)
颜色