Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

r - Unable to generate a legend in ggplot2 for mixed geoms

I'm using ggplot2 to generate a plot with a geom_line and a geom_bar, separate colors, with a legend indicating what each is.

Following the advice in:

  1. Why ggplot2 legend not show in the graph
  2. How to add a legend for two geom layers in one ggplot2 plot?
  3. Why ggplot2 legend not show in the graph

I wrote this code

ggplot(data = full_df, mapping = aes(x = year)) +
            geom_line(aes(y = pop, color = 'label1'), color = 'black') +
            geom_bar(aes(y = alive, fill = 'label2'),
                     stat = 'identity',
                     fill = 'skyblue') +
            scale_color_manual(name = '', values = c('black')) +
            scale_fill_manual(name = '', values = c('skyblue'))

but that gives me

enter image description here

which does not contain a legend. I'm very confused about what I've done wrong.

Thank you so much! The dput of the data is:

structure(list(year = 1900:2018, alive = c(0.03714, 0.05532, 
0.12392, 0.24968, 0.46074, 0.8255, 1.51661, 2.72924, 4.74468, 
7.78292, 14.27244, 26.53592, 72.13076, 124.30589, 227.8309, 385.87374, 
544.5324, 756.72672, 1060.19694, 1276.04376, 1730.9076, 2245.02075, 
2750.56397, 3359.862, 4037.9985, 4646.08125, 5250.4848, 5998.49614, 
6493.083, 6940.09888, 7666.40316, 7784.3843, 8153.3767, 8135.17205, 
8666.09646, 9226.58368, 9860.0216, 10236.33393, 10918.2816, 11325.80761, 
12048.8953, 13527.7363, 15773.71432, 16493.49592, 15678.817, 
15770.05136, 18785.6788, 21707.1338, 21528.43472, 21555.16944, 
22395.86976, 23657.23875, 24260.28605, 25190.0196, 27154.4596, 
27705.27408, 28955.47915, 28355.88129, 27012.0575, 26667.27525, 
27022.72144, 26898.68664, 27616.20948, 27751.48304, 28302.25489, 
26636.40186, 25179.53412, 24280.76448, 23858.57448, 24681.8712, 
25266.7138, 23245.0734, 21355.60932, 20327.80307, 20859.9308, 
21197.87111, 23095.16826, 25474.84416, 25690.84075, 27663.29148, 
29190.29341, 29786.1546, 30094.902, 29567.61416, 29649.5946, 
29392.00968, 28078.08125, 27798.32487, 28300.28402, 29176.30408, 
29449.21616, 29188.0939, 27321.633, 26475.57225, 25248.3344, 
24898.4211, 24345.31146, 23232.14968, 22849.1648, 22899.7986, 
22548.95148, 21754.45494, 21655.48312, 21144.4674, 19927.24074, 
19028.3766, 18267.64633, 17173.08892, 16420.99335, 14787.4185, 
13708.63662, 12853.73102, 12456.47819, 12145.4403, 12021.01002, 
11404.57806, 10845.10658, 10372.66932, 9629), pop = c(3714L, 
2766L, 3098L, 3121L, 3291L, 3302L, 3527L, 3844L, 4162L, 4348L, 
5228L, 6488L, 12062L, 14471L, 18829L, 23051L, 23883L, 24762L, 
26262L, 24264L, 25590L, 26181L, 25699L, 25560L, 25425L, 24615L, 
23760L, 23506L, 22313L, 21164L, 20982L, 19330L, 18565L, 17161L, 
17102L, 17183L, 17456L, 17323L, 17736L, 17719L, 18209L, 19802L, 
22424L, 22829L, 21175L, 20818L, 24280L, 27515L, 26806L, 26404L, 
27024L, 28155L, 28513L, 29272L, 31235L, 31578L, 32731L, 31813L, 
30097L, 29525L, 29744L, 29448L, 30084L, 30094L, 30563L, 28653L, 
26988L, 25936L, 25402L, 26196L, 26735L, 24524L, 22468L, 21331L, 
21836L, 22139L, 24069L, 26496L, 26671L, 28669L, 30203L, 30774L, 
31050L, 30467L, 30515L, 30216L, 28835L, 28519L, 29006L, 29876L, 
30128L, 29834L, 27900L, 27009L, 25730L, 25345L, 24754L, 23596L, 
23183L, 23212L, 22836L, 22014L, 21899L, 21371L, 20133L, 19220L, 
18449L, 17342L, 16581L, 14930L, 13839L, 12974L, 12571L, 12255L, 
12127L, 11502L, 10934L, 10452L, 9629L)), class = "data.frame", row.names = c(NA, 
-119L))
question from:https://stackoverflow.com/questions/65895663/unable-to-generate-a-legend-in-ggplot2-for-mixed-geoms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Remove the additional color and fill values from geom_line and geom_bar :

library(ggplot2)

ggplot(full_df, mapping = aes(x = year)) +
  geom_line(aes(y = pop, color = 'label1')) +
  geom_bar(aes(y = alive, fill = 'label2'),
           stat = 'identity') +
  scale_color_manual(name = '', values = 'black') +
  scale_fill_manual(name = '', values = 'skyblue')

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...