R ggplot没有向我显示图例
我试图在情节上添加一个传奇,但它不起作用。以下是我的示例数据集:R ggplot没有向我显示图例,r,ggplot2,legend,legend-properties,R,Ggplot2,Legend,Legend Properties,我试图在情节上添加一个传奇,但它不起作用。以下是我的示例数据集: 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"), aver
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))
这是它的R代码,用于ggplot2:
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")
我假设您在提供的示例中错误地命名了数据,
average
为percent\u occ
,avg\u arrivals
为percent\u arrivals
。运行示例代码会导致以下错误:
grDevices::col2rgb(颜色,TRUE)中出错:无效的颜色名称“条”
这来自以下方面:
几何图形条(stat=“identity”,alpha=0.7,width=0.50,fill=“bar”)
问题是ggplot正在查找颜色条
,这不是有效的颜色名称。如果希望根据变量名为值指定颜色,则需要在aes()
参数中指定颜色
在您的示例中使用此选项:
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"))
如果希望使用aes()
定义映射到美学的变量的颜色,则必须添加颜色比例命令以覆盖ggplot默认托盘。例如,包括以下内容:
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"))
您的绘图代码不能与数据集一起为我运行(缺少变量,如
percent\u occ
)。你能澄清一下你希望这个传奇是什么样子吗?您当前的代码看起来像是在为线条绘制图例,而不是为任何其他层绘制图例。此外,最好删除ggplot中与问题实际无关的所有方面。所有的主题选项、标签和比例都是多余的,只会使人们更难看到错误的来源。您好,谢谢您的帮助。是的,@aosmith-我确实增加了限制-从7.5增加到50。现在您可能会看到这些条。@米奇·哈珀-我确实删除并最小化了我的代码。@GabrielBurcea谢谢你的修改,看起来好多了!对于填充和颜色,您参考的“条”和“线”是什么?谢谢您的帮助。我希望能以某种方式保持我所给的颜色:黄色4表示条形,黑色表示线条。有什么办法可以保存它们吗?我试着把图例放在情节下面,但没有这样做是的,没错,我做了更改,这就是为什么我有其他变量。更新以解释如何设置颜色。如果您对答案满意,请向上投票并标记为已接受:)