[英]ggplot does not show me the legend
I am trying to add a legend on the plot but it is not working. 我正在尝试在剧情上添加图例,但它不起作用。 Here is my example dataset:
这是我的示例数据集:
structure(list(hour = c("0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "20", "21", "22", "23"),
average = c(2.61835748792271, 2.11352657004831,
1.71497584541063, 1.40338164251208,
1.15700483091787, 1.86376811594203,
1.83574879227053, 1.83478260869565,
1.1256038647343, 1.7512077294686,
2.4951690821256, 2.08695652173913,
3.52898550724638,3.85990338164251,
3.96376811594203, 4.00968523002421,
3.9225181598063, 3.96610169491525,
3.89588377723971, 3.95883777239709,
3.81884057971014, 3.71497584541063, 4.5,
3.08454106280193),
avg_arrivals = c(2.71428571428571, 1.91666666666667,
1.30612244897959, 1.38, 1.85106382978723,
1.79583333333333, 1.14285714285714,
2.93877551020408, 3.33333333333333,
4.82456140350877, 6.03448275862069,
6.47368421052632, 6.53448275862069,
6.48275862068965, 5.77586206896552,
6.49122807017544, 6.37931034482759,
5.89655172413793, 5.70689655172414,
6.17241379310345, 5.77586206896552,
4.27586206896552, 4.1551724137931,
2.7719298245614)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -24L))
Here is the R code for it and used ggplot2: 这是用于ggplot2的R代码:
ggplot(data, aes(x = as.numeric(hour), y = percent_occ,
group = hour)) +
geom_bar(stat = "identity", alpha=0.7, width = 0.50, fill="bars" ) +
geom_point(aes(y = percent_arrivals)) +
geom_line(aes(y = percent_arrivals, group = 1, color =
"line")) + # color = "line
theme(legend.position = "bottom", legend.box = "horizontal")
I am assuming you have misnamed the data in the provided example, with average
being percent_occ
and avg_arrivals
being percent_arrivals
. 我假设你已经在提供的示例名不副实的数据,以
average
存在percent_occ
和avg_arrivals
是percent_arrivals
。 Running your example code results in the error: 运行示例代码会导致错误:
Error in grDevices::col2rgb(colour, TRUE) : invalid color name 'bars'
grDevices :: col2rgb(colour,TRUE)中的错误:无效的颜色名称'bars'
This comes from the following: 这来自以下方面:
geom_bar(stat = "identity", alpha=0.7, width = 0.50, fill="bars")
The problem is that ggplot is looking for the colour bars
, which is not a valid colour name. 问题在于ggplot正在寻找颜色
bars
,这不是有效的颜色名称。 If you instead want a colour to be assigned to the value based on a variable name, you need to specify it within the aes()
argument. 相反,如果您希望根据变量名将颜色分配给值,则需要在
aes()
参数中指定颜色。
Using this in your example: 在您的示例中使用以下代码:
ggplot(data, aes(x = as.numeric(hour), y = percent_occ, group = hour)) +
geom_bar(stat = "identity", alpha=0.7, width = 0.50, aes(fill="bars")) +
geom_point(aes(y = percent_arrivals)) +
geom_line(aes(y = percent_arrivals, group = 1, color = "line"))
If you wish to define the colour of a variable which is mapped to an aesthetic using aes()
, you will have to add a colour scale command to override the ggplot default pallete. 如果希望使用
aes()
定义映射到美学的变量的颜色,则必须添加一个色标命令以覆盖ggplot默认调色板。 For your example including these: 对于您的示例包括:
ggplot(data, aes(x = as.numeric(hour), y = percent_occ, group = hour)) +
geom_bar(stat = "identity", alpha=0.7, width = 0.50, aes(fill="bars")) +
geom_point(aes(y = percent_arrivals)) +
geom_line(aes(y = percent_arrivals, group = 1, color = "line")) +
scale_colour_manual(values = c("black")) +
scale_fill_manual(values = c("yellow4"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.