英文:
Can't change legend title of ggplot
问题
无法更改这个图例标题!
我一直在尝试更改图例标题,但没有任何效果。我已经浏览了这些论坛,人们建议的一切都没有效果(指南、操纵诸如scale_color_continuous
等的东西等)。
另一个问题是,在geom_point
的aes
命令中的“size”函数显示在图例中(即,在我的实际数据的形状下给出了“size”命令)。
我已经努力尝试解决这些问题,所以任何帮助都将不胜感激。
以下是生成图表的可重现代码:
## 创建数据框
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)
## 定义因子并重新排序TimePoint的级别
dataframe$TimePoint <- as.factor(dataframe$TimePoint)
dataframe$Predictor <- as.factor(dataframe$Predictor)
dataframe$TimePoint = factor(dataframe$TimePoint, levels = c("Pre", "Post"), ordered = TRUE)
## 创建绘图
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)) + ## 指定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")
英文:
Can't change this legend title!
I have been trying to change the legend title, but nothing has worked. I've gone through these forums and every thing people have suggested has not worked (guides, manipulating things like scale_color_continuous, etc.).
The other issue is that the "size" function in the geom_point aes command shows in the legend (i.e., it gives a shape to the size command under the shapes for my actual data).
I've been trying for hours to fix these issues, so any help would be appreciated.
Here is the reproducible code to produce the chart:
## 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)
I have tried every recommendation I have found on here. Things like the following:
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")
答案1
得分: 0
请尝试以下代码,
将aes()
中的size
移除,并将其放在geom_point
之外
在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)
英文:
Please try the below code,
remove the size
from aes()
and keep it outside aes()
in geom_point
use shape
in guides
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)
答案2
得分: 0
尺寸问题:
传递给 aes()
的参数是将数据映射到图形的美学元素。当美学元素不应与数据映射时,它只是一个常数参数(例如 size = 2
与 shape = Predictor
),您希望将参数传递给 aes()
外部:
geom_point(aes(shape=Predictor), size=2)
图例名称问题:
此外,每个美学元素(颜色、填充、大小、透明度等)都有自己的图例,您必须指定要更改的图例。在您在最后给出的示例中,您正在操作 color 美学元素,但您想要操作 shape 美学元素:
ggp + scale_shape_discrete(name = "新的图例标题")
ggp + labs(shape = "新的图例标题")
ggp + guides(col = guide_shape(title = "新的图例标题"))
额外信息:
线条宽度的 size
参数已弃用,请改用 linewidth
:
theme(axis.line = element_line(linewidth = .6, colour = "black", linetype=1))
英文:
Size problem:
Arguments passed to aes()
are mappings of your data to the aesthetic elements of the graph. When the aesthetic isn't supposed to be mapped with your data, it is just a constant argument (i.e. size = 2
versus shape = Predictor
), you want to pass the argument outside aes()
:
geom_point(aes(shape=Predictor), size=2)
Legend name problem:
Also, each aesthetic (color, fill, size, alpha, etc.) has it's own legend, and you have to specify which one you're wanting to change. In the examples you gave at the end, you were manipulating the color aes., but you want to manipulate the 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"))
Extra:
The size
argument for width of a line is deprecated, use linewidth
instead:
theme(axis.line = element_line(linewidth = .6, colour = "black", linetype=1))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论