【问题标题】:how to add legend in R to a ggplot如何将R中的图例添加到ggplot
【发布时间】:2014-12-23 19:33:34
【问题描述】:

我对R有疑问,这里应该有问题,我想为两个不同的变量添加一个图例,一个是红色的“Insgesamt_ALL”,另一个是黑色的“weiblich_ALL”。

> data<-read.csv("DatensatzUE4_01.csv",sep=";",header=T)

> nDATA<-subset(data,data$Fach=='Wirtschaftsingenieurw.m.wirtschaftswiss.Schwerpkt.')
> 
> library(ggplot2)
> 
> p <- ggplot(data=nDATA,aes(x=Semester,fill=y))
+     ggtitle("GGplot")
+     xlab("Year and Semester")
+     ylab("Total of the student")
+     geom_bar(size=3,aes(y=Insgesamt_ALL,fill=Semester),stat="identity",fill="red")
+     geom_bar(size=3,aes(y=weiblich_ALL,fill=Semester),stat="identity",fill="black")
> 
> p2<-p+ 
    theme(panel.background=element_rect(fill="white"),
          panel.grid.major=element_line(colour="grey",size=0.1),
          plot.title=element_text(face="bold"),
          axis.text=element_text(colour="black"),
          axis.title.x=element_text(face="bold"),
           axis.text.x=element_text(angle=90) )
> 
> plot(p2)

结果:

【问题讨论】:

  • 缺少图例的原因是您使用fill="red"fill="black" 将填充固定为常数值。仅当您将填充用作美学时,才会自动打印图例。您没有按预期使用ggplot(),我鼓励您从一些介绍中正确学习。
  • 使用{} 将您的代码放入代码块中。不要不要为此使用"
  • 另外,您应该提供您的数据,以便示例可重现。最好的方法是发布dput(data) 的输出。如果数据集非常大,请将其上传到某处并发布链接。
  • 谢谢你的提醒 :)

标签: r ggplot2 legend r-factor geom-bar


【解决方案1】:

您缺少图例的基本问题是您应该通过将 ggplot 填充美学映射到变量来利用它。之后,您可以根据需要修改填充颜色。您有 2 个变量:Insgesamt_ALLweiblich_ALL

首先让我们构建一些模仿您的实际数据集的假数据(请参阅@jlhoward 评论):

(tmp_data <- data.frame(Semester = seq(1:12), Insgesamt_ALL = sample(0:3000, 12),     weiblich_ALL = sample(2000:5000, 12)))

   Semester Insgesamt_ALL weiblich_ALL
1         1          2264         2643
2         2           244         3742
3         3          1681         2897
4         4          1037         4342
5         5          1225         4384
6         6           478         2195
7         7            97         2948
8         8          2537         3509
9         9          1210         3892
10       10          2016         2507
11       11          2524         2415
12       12           427         4167

第一个关键点是你应该为 ggplot 提供一组键/值观察,所以让我们重塑数据集:

library(tidyr)
nDATA    <- gather(tmp_data, variable, count_of_student, Insgesamt_ALL, weiblich_ALL)

我在这里使用了tidyr::gather,但任何其他工具也可以。让我们直接绘制它:

library(ggplot2)
p <- ggplot(nDATA) + geom_bar(aes(x = Semester, y = count_of_student, fill = variable), stat = "identity")
plot(p)

您所追求的基本上是将填充比例更改为自定义颜色(黑色和红色):

fill_colors        <- c("#000000", "#FF0000")
names(fill_colors) <- levels(nDATA$variable)
fill_scale <- scale_fill_manual(name = "Variable", values = fill_colors)

p + 填充比例

最后,让我们通过重新排序variable 因子的级别来切换黑色和红色填充颜色:

nDATA$variable         <- relevel(nDATA$variable, ref = "weiblich_ALL")
new_fill_colors        <- fill_colors
names(new_fill_colors) <- levels(nDATA$variable)
new_fill_scale <- scale_fill_manual(name = "Variable", values = new_fill_colors)
p + new_fill_scale

你现在应该走在正确的轨道上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2021-05-21
    • 2018-04-13
    • 1970-01-01
    相关资源
    最近更新 更多