简体   繁体   English

在 ggplot 中编辑图例(文本)标签

[英]Editing legend (text) labels in ggplot

I have spent hours looking in the documentation and on StackOverflow, but no solution seems to solve my problem.我花了几个小时查看文档和 StackOverflow,但似乎没有解决方案可以解决我的问题。 When using ggplot I can't get the right text in the legend, even though it's in my dataframe.使用ggplot我无法在图例中获得正确的文本,即使它在我的数据框中。 I have tried scale_colour_manual , scale_fill_manual with different values for labels= such as c("T999", "T888")", "cols" .我已经尝试了scale_colour_manualscale_fill_manuallabels=不同的值,例如c("T999", "T888")", "cols"

Here is my code:这是我的代码:

T999 <- runif(10, 100, 200)
T888 <- runif(10, 200, 300)
TY <- runif(10, 20, 30)
df <- data.frame(T999, T888, TY)


ggplot(data = df, aes(x=T999, y=TY, pointtype="T999")) + 
       geom_point(size = 15, colour = "darkblue") + 
       geom_point(data = df, aes(x=T888, y=TY), colour = 'red', size = 10 ) + 
       theme(axis.text.x = element_text(size = 20), axis.title.x =element_text(size = 20),   axis.text.y = element_text(size = 20)) +
       xlab("Txxx") + ylab("TY [°C]") + labs(title="temperatures", size = 15) + 
       scale_colour_manual(labels = c("T999", "T888"), values = c("darkblue", "red")) +    theme(legend.position="topright")

Help would be very appreciated!帮助将不胜感激!

The tutorial @Henrik mentioned is an excellent resource for learning how to create plots with the ggplot2 package.提到的教程@Henrik 是学习如何使用ggplot2包创建绘图的极好资源。

An example with your data:您的数据示例:

# transforming the data from wide to long
library(reshape2)
dfm <- melt(df, id = "TY")

# creating a scatterplot
ggplot(data = dfm, aes(x = TY, y = value, color = variable)) + 
  geom_point(size=5) +
  labs(title = "Temperatures\n", x = "TY [°C]", y = "Txxx", color = "Legend Title\n") +
  scale_color_manual(labels = c("T999", "T888"), values = c("blue", "red")) +
  theme_bw() +
  theme(axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 16),
        axis.text.y = element_text(size = 14), axis.title.y = element_text(size = 16),
        plot.title = element_text(size = 20, face = "bold", color = "darkgreen"))

this results in:这导致:

在此处输入图片说明

As mentioned by @user2739472 in the comments: If you only want to change the legend text labels and not the colours from ggplot's default palette, you can use scale_color_hue(labels = c("T999", "T888")) instead of scale_color_manual() .正如@user2739472 在评论中提到的:如果您只想更改图例文本标签而不是 ggplot 默认调色板中的颜色,您可以使用scale_color_hue(labels = c("T999", "T888"))而不是scale_color_manual()

The legend titles can be labeled by specific aesthetic .图例标题可以用特定的审美来标记。

This can be achieved using the guides() or labs() functions from ggplot2 (more here and here ).这可以使用ggplot2guides()labs()函数来ggplot2 (更多在这里这里)。 It allows you to add guide/legend properties using the aesthetic mapping.它允许您使用美学映射添加指南/图例属性。

Here's an example using the mtcars data set and labs() :这是使用mtcars数据集和labs()的示例:

ggplot(mtcars, aes(x=mpg, y=disp, size=hp, col=as.factor(cyl), shape=as.factor(gear))) +
  geom_point() +
  labs(x="miles per gallon", y="displacement", size="horsepower", 
       col="# of cylinders", shape="# of gears")

在此处输入图片说明

Answering the OP's question using guides() :使用guides()回答 OP 的问题:

# transforming the data from wide to long
require(reshape2)
dfm <- melt(df, id="TY")

# creating a scatterplot
ggplot(data = dfm, aes(x=TY, y=value, color=variable)) + 
  geom_point(size=5) +
  labs(title="Temperatures\n", x="TY [°C]", y="Txxx") +
  scale_color_manual(labels = c("T999", "T888"), values = c("blue", "red")) +
  theme_bw() +
  guides(color=guide_legend("my title"))  # add guide properties by aesthetic

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM