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

标签 r text ggplot2 label

我花了几个小时查看文档和 StackOverflow,但似乎没有解决方案可以解决我的问题。使用 ggplot 时,我无法在图例中获得正确的文本,即使它位于我的数据框中。我尝试过使用不同的 labels= 值,例如 c("T999", "T888")", “列”

这是我的代码:

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")

这是上述代码的图形输出:

graphical output of ggplot code

非常感谢您的帮助!

最佳答案

@Henrik 提到的教程是学习如何使用 ggplot2 创建绘图的绝佳资源。包。

您的数据示例:

# 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"))

这会导致:

enter image description here

正如 @user2739472 在评论中提到的:如果您只想更改图例文本标签而不是 ggplot 默认调色板中的颜色,则可以使用 scale_color_hue(labels = c("T999", "T888"))而不是scale_color_manual() .

关于r - 在 ggplot 中编辑图例(文本)标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23635662/

相关文章:

r - 使用grep函数进行文本挖掘

r - 拟合 ggplot2、geom_smooth 和 nls

java - 如何从 Java Stream 中获取以该数字开头的数字的每个数字?

r - 在ggplot2中仅绘制x和y轴边界(无框)?

r - 如何将wordcloud放入grob中?

r - 如何将 geom_point() 添加到 autolayer() 行?

r - bookdown文档中的直方图出现两次

python - 将文本文件导入mysqldb

text - 是否可以用作者信息补充朴素贝叶斯文本分类算法?

r - 如何在 R 中的 ggplot2 中绘制混合效应模型估计值?