如何在ggplot中的图例中命名数据? [英] How to name data in legend in a ggplot?

查看:201
本文介绍了如何在ggplot中的图例中命名数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据框One_APD:

One_APD = structure(list(Amplification = c(108.91, 120.765, 134.875, 151.877, 172.812, 199.324), Voltage = c(351.955, 353.954, 355.956, 357.955, 359.947, 361.948), pred = c(1.54580502813059, 1.56713437847747, 1.58992216028315, 1.61410007849728, 1.63960908075698, 1.66665619275778)), .Names = c("Amplification", "Voltage", "pred"), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"))

它看起来像这样:

> One_APD
  Amplification Voltage     pred
1       108.910 351.955 1.545805
2       120.765 353.954 1.567134
3       134.875 355.956 1.589922
4       151.877 357.955 1.614100
5       172.812 359.947 1.639609
6       199.324 361.948 1.666656

并将其绘制如下:

ggplot(One_APD, aes(x = Voltage, y = log(log(Amplification)))) +
  geom_point(size=3, colour="blue") +
  geom_line(aes(y = pred), size=2, alpha=0.3, colour="red")

如下所示:

我想添加一个图例,命名为蓝点和红色曲线. 据我所知,所有图例命令都针对ggplot的es中的填充或颜色.但我不提供填充或颜色,因为分配颜色或填充没有意义. 如何将对应的图例添加到绘图中的数据?

I would like to add a legend which names the blue points and the red curve. As far as I see all legend commands aim at fill or colour in the aes of ggplot. But I don't provide fill or colour because it makes no sense to assign colour or fill.. How can I add corresponding legends to the data in the plot?

解决方案位于为复杂的图构建手动图例

推荐答案

我认为下图可以回答您的问题并符合您的要求

I think the following graph answers your question and matches your requirements

colors=c("Amplification"="blue","pred"="red")
ggplot(data=One_APD,aes(x=Voltage)) + 
  geom_point(aes(y=log(log(Amplification)), colour="Amplification"),size=3) +
  geom_line(aes(y=pred,colour="pred"), size=2, alpha=0.3) +scale_colour_manual(name="Variable",values=colors) +
  ylab("log(log(Amplification)") + xlab("Voltage") 

这篇关于如何在ggplot中的图例中命名数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆