Home Articles

如何在ggplot中命名图例中的数据?

Asked
Viewed 1642 times
4

我有数据框 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

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

编辑:解决方案在Construct a manual legend for a complicated plot

1 Answer

  • 2

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

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

    enter image description here

Related