I can't stop ggplot2 producing two legends instead of just one - help !

Hi there,

i'm a newbie at posting on this forum. so hopefully my question is clear/ simple.

i am trying to do a simple logged ggplot, showing the change in tree and shrub density over time (site age). the tree species are split into native / exotic.

i have also downloaded the "viridis" package - to enable a type of coloration to the legend+line+points+confidence interval fill.

this is the code i have used:

attach(data.df4)
base <- ggplot(data.df4, aes(age, total_trees, colour=status))
base + theme_classic(base_size = 10, base_family = "Bell MT") +
scale_y_log10() +
geom_point(aes(color = status)) +
geom_smooth(aes(color=status, fill=status), method = "lm",se=TRUE) +
scale_color_viridis(discrete = TRUE, option = "D")+
scale_fill_viridis(discrete = TRUE, option = "D") +
labs(title = "changes in planted canopy and subcanopy tree and shrub density over time", x="planting age", y="density (plot-level)")

The problem is, when i do this i get two sepperate legends. which i don't want. i can't figure out how to keep the viridis legend, and remove the other legend.

what i want to achieve is shown at this website under " Viridis color palettes": https://www.datanovia.com/en/blog/ggplot-colors-best-tricks-you-will-love/

what i am producing:

Sample of Data table:
Site - Status - Age - Total Trees
site1 - native -- 15 ---- 10
site1 - exotic -- 15 ----- 2
site2 - native -- 20 ----- 35
site2 - exotic -- 20 ----- 4

I tried to reproduce your problem and I get only one legend. I had to make up data, so the difference probably lies in the data details. Can you post a small data set that results in the problem?
(I removed the setting of base_family to avoid a bunch of warnings about the font family)

library(ggplot2)
library(viridis)
#> Warning: package 'viridis' was built under R version 3.5.3
#> Loading required package: viridisLite
data.df4 <- data.frame(age = c(10, 10, 10, 20, 20,20, 30, 30,30, 60, 60, 60),
                       total_trees = c(100, 10, 34, 125, 13, 46, 150, 17, 52, 180, 20, 65),
                       status = rep(c("native", "exotic", ""),4))

base <- ggplot(data.df4, aes(age, total_trees, colour=status))
base + theme_classic(base_size = 10) +
  scale_y_log10() +
  geom_point(aes(color = status)) +
  geom_smooth(aes(color=status, fill=status), method = "lm",se=TRUE) +
  scale_color_viridis(discrete = TRUE, option = "D")+
  scale_fill_viridis(discrete = TRUE, option = "D") +
  labs(title = "changes in planted canopy and subcanopy tree and shrub density over time", 
       x="planting age", y="density (plot-level)")

Created on 2019-05-10 by the reprex package (v0.2.1)

1 Like

thank you!

i've just added to the query a layout of the data

ahhhhh - i just got it to work!

i just checked my data - and at the bottom of it, it had a "grand total" column which shouldn't have been there (forgot to delete it before converting it to csv)

but i also at the top my r.studio screen had two of these listed:
data.df4 <-read.csv("total_native_exotic_density_data.csv")
data.df4 <-read.csv("total_native_exotic_richness_data.csv") (i was supposed to change this to df5)

so i was reading two data.df4 instead of one

i think this must be why?

thanks very much - if you hadn't mentioned checking the data, i probably wouldn't have seen the double up in dataframe runs.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.