Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.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 带有geom_文字和geom_点的颜色图例_R_Ggplot2_Legend_Ggrepel_Geom Point - Fatal编程技术网

R 带有geom_文字和geom_点的颜色图例

R 带有geom_文字和geom_点的颜色图例,r,ggplot2,legend,ggrepel,geom-point,R,Ggplot2,Legend,Ggrepel,Geom Point,图例标签旁边的点如何着色?缩放颜色手动或缩放填充手动不起作用。另外,如何将图例中的点更改为正方形?谢谢 set.seed(1) library(ggplot2) library(ggrepel) df <- data.frame(n=runif(8),y=1:8,l=letters[1:8],col=palette.colors(8)) p_vol <- ggplot(df, aes(n, y, label = l)) + geom_point(aes(fill=l),colo

图例标签旁边的点如何着色?缩放颜色手动或缩放填充手动不起作用。另外,如何将图例中的点更改为正方形?谢谢

set.seed(1)
library(ggplot2)
library(ggrepel)
df <- data.frame(n=runif(8),y=1:8,l=letters[1:8],col=palette.colors(8))

p_vol <- ggplot(df, aes(n, y, label = l)) +
  geom_point(aes(fill=l),color = df$col) +
  geom_text_repel(col=df$col)+theme_classic()
print(p_vol)
set.seed(1)
图书馆(GG2)
图书馆(ggrepel)

df您需要在
geom_point()
,设置
color=l
中的美学调用中包含color参数。然后您可以使用
scale\u color\u manual
使用所需的颜色

p_vol <- ggplot(df, aes(n, y, label = l)) +
  geom_point(aes(fill=l, color = l)) +
  geom_text_repel(col=df$col) +
  theme_classic() +
  scale_color_manual(values = df$col)

p\u vol您需要在
geom\u point()
中的美学调用中包含颜色参数,设置
color=l
。然后您可以使用
scale\u color\u manual
使用所需的颜色

p_vol <- ggplot(df, aes(n, y, label = l)) +
  geom_point(aes(fill=l, color = l)) +
  geom_text_repel(col=df$col) +
  theme_classic() +
  scale_color_manual(values = df$col)

p\u vol您还可以尝试使用数据框中的颜色从
aes()
启用
fill
。这里是代码,我使用了不同的颜色,因为我对函数
调色板没有足够的了解。还可以使用
scale\u fill\u identity()。代码如下:

set.seed(1)
library(ggplot2)
library(ggrepel)
library(RColorBrewer)
df <- data.frame(n=runif(8),y=1:8,l=letters[1:8],col=rainbow(8))
#Plot
ggplot(df, aes(n, y, label = l,color=l,fill=col)) +
  geom_point() +
  geom_text_repel(show.legend = F)+theme_classic()+
  scale_fill_identity()
set.seed(1)
图书馆(GG2)
图书馆(ggrepel)
图书馆(RColorBrewer)

df您还可以尝试使用数据框中的颜色从
aes()
启用
fill
。这里是代码,我使用了不同的颜色,因为我对函数
调色板没有足够的了解。还可以使用
scale\u fill\u identity()。代码如下:

set.seed(1)
library(ggplot2)
library(ggrepel)
library(RColorBrewer)
df <- data.frame(n=runif(8),y=1:8,l=letters[1:8],col=rainbow(8))
#Plot
ggplot(df, aes(n, y, label = l,color=l,fill=col)) +
  geom_point() +
  geom_text_repel(show.legend = F)+theme_classic()+
  scale_fill_identity()
set.seed(1)
图书馆(GG2)
图书馆(ggrepel)
图书馆(RColorBrewer)

df不建议调用
ggplot
中带有
df$
的列。建议将
col
列重命名为
colors
或其他非参数。不建议在
ggplot
中调用带有
df$
的列。建议将
col
列重命名为
colors
或其他非参数。我发现填充是不必要的,事实上scale\u fill\u identity()即使没有填充,也使用标准的ggplot颜色,而不是数据框中的颜色。我发现填充是不必要的,事实上scale\u fill\u identity()即使没有填充,也使用标准ggplot颜色,而不是数据框中的颜色