R语言 无法更改ggplot的图例标题

5sxhfpxr  于 2023-06-27  发布在  其他
关注(0)|答案(2)|浏览(138)

无法更改此图例标题!

我一直试图改变传奇的标题,但没有任何工作。我浏览了这些论坛,人们建议的每一件事都没有起作用(指南,操纵像scale_color_continuous这样的东西,等等)。
另一个问题是geom_point aes命令中的“size”函数显示在图例中(即,它为我的实际数据的形状下的size命令提供了一个形状)。
我已经花了几个小时来解决这些问题,所以任何帮助都将不胜感激。
下面是生成图表的可重复代码:

## Create data frame
Predictor <- c("No", "Yes", "No", "Yes")
TimePoint <- c("Pre", "Pre", "Post", "Post")
value <- c(33, 32, 31, 40)
SE <- c(1.5, 1.5, 1.5, 1.5)
dataframe <- data.frame(Predictor, TimePoint, estimate, SE)

## Define factors and reorder levels of TimePoint
dataframe$TimePoint <- as.factor(dataframe$TimePoint)
dataframe$Predictor <- as.factor(dataframe$Predictor)
dataframe$TimePoint = factor(dataframe$TimePoint, levels = c("Pre", "Post"), ordered = TRUE)

## Create Plot
ggp <- ggplot(data=dataframe, aes(x=TimePoint, y=value, group=Predictor)) +
  geom_errorbar(aes(ymin=value-SE, ymax=value+SE), width=.1) +
  geom_line()+
  geom_point(aes(shape=Predictor, size=2)) +
  coord_cartesian(ylim = c(0, 50)) + ## specifies upper and lower of y
  ylab("Value") +
  xlab("
       Timing")+
  scale_x_discrete(expand = c(0,0.25)) +
  scale_y_continuous(breaks = seq(0, 50, 5), limits = c(0, 50), expand = c(0, 0)) +
  theme_classic() +
  theme(axis.line = element_line(size = .6, colour = "black", linetype=1),         
        axis.text = element_text( angle = 0, color="black", size=18, face=2),
        axis.title.x = element_text(color="black", size=18, face=2),
        axis.title.y = element_text(color="black", size=18, face=2, margin=margin(r=20)))

print(ggp)

我已经尝试了我在这里找到的每一个建议。下面是一些例子:

ggp + scale_color_discrete(name = "New Legend Title")

ggp + labs(color = "New Legend Title")

ggp + scale_fill_discrete(name="New Legend Title")

ggp + guides(col = guide_color(title = "New Legend Title"))

ggp + labs(color = "New Legend Title")
ssgvzors

ssgvzors1#

尺寸问题:

传递给aes()的参数是您的数据到图的美学元素的Map。当美学不应该与您的数据Map时,它只是一个恒定的参数(即size = 2shape = Predictor),您希望将参数传递到aes()外部:

geom_point(aes(shape=Predictor), size=2)

图例名称问题:

此外,每个美学(颜色,填充,大小,阿尔法等)都有自己的图例,您必须指定要更改的图例。在你最后给出的例子中,你正在操作 color aes.,但是你想操作 shape aes.:

  • ggp + scale_shape_discrete(name = "New Legend Title")
  • ggp + labs(shape = "New Legend Title")
  • ggp + guides(col = guide_shape(title = "New Legend Title"))
    额外:

不推荐使用size参数表示行宽,请使用linewidth

theme(axis.line = element_line(linewidth = .6, colour = "black", linetype=1))
at0kjp5o

at0kjp5o2#

请尝试下面的代码
aes()中删除size,并将其保留在geom_point中的aes()之外使用guides中的shape

ggp <- ggplot(data=dataframe, aes(x=TimePoint, y=value, group=Predictor)) +
  geom_errorbar(aes(ymin=value-SE, ymax=value+SE), width=.1) +
  geom_line()+
  geom_point(aes(shape=Predictor), size=4) +
  coord_cartesian(ylim = c(0, 50)) + ## specifies upper and lower of y
  ylab("Value") +
  xlab("
       Timing")+
  scale_x_discrete(expand = c(0,0.25)) +
  scale_y_continuous(breaks = seq(0, 50, 5), limits = c(0, 50), expand = c(0, 0)) +
  theme_classic() +
  theme(axis.line = element_line(size = .6, colour = "black", linetype=1),         
        axis.text = element_text( angle = 0, color="black", size=18, face=2),
        axis.title.x = element_text(color="black", size=18, face=2),
        axis.title.y = element_text(color="black", size=18, face=2, margin=margin(r=20))) +
  guides(shape=guide_legend(title="New Legend Title"))

print(ggp)

相关问题