0

I am trying to make a barplot where the bars would have the color "dodgerblue4" as a gradient. I tried to use scale_color_gradient but it didn't work. I would like the highest value to have "dodgerblue4" and have that color fading, is it possible? Thank you! My code for now is:

Fields<-c("Oceanography","Meteorology \n atmospheric \n Science",
          "Physical sciences \n other topics","Environmental \n sciences \n ecology",
          "Geography","Marine \n freshwater \n biology","Mathematics","Geology",
          "Zoology", "Geochemestry \n and Geophysics")
n_art<-c(3178,3166,2919,1975,1963,1900,1027,1015,892,840)
n_field<-c(1,2,3,4,5,6,7,8,9,10)
df_wos<- data.frame(n_art,Fields,n_field)
df_wos$Fields<-factor(df_wos$Fields,
                      levels = df_wos$Fields[order(df_wos$n_art,decreasing = TRUE)])

ggplot(df_wos) +
   aes(x = Fields, weight = n_art) +
   geom_bar(fill = "dodgerblue4")+
   scale_fill_hue() +
   labs(x=NULL, y = "Number of papers") +
   theme_bw()+
   theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))

1 Answer 1

1

There are multiple ways that you could interpret 'gradient' in this case.

Since you're talking about a 'fade', you could map the y-value of a bar to the alpha aesthetic.

library(ggplot2)

Fields<-c("Oceanography","Meteorology \n atmospheric \n Science",
          "Physical sciences \n other topics","Environmental \n sciences \n ecology",
          "Geography","Marine \n freshwater \n biology","Mathematics","Geology",
          "Zoology", "Geochemestry \n and Geophysics")
n_art<-c(3178,3166,2919,1975,1963,1900,1027,1015,892,840)
n_field<-c(1,2,3,4,5,6,7,8,9,10)
df_wos<- data.frame(n_art,Fields,n_field)
df_wos$Fields<-factor(df_wos$Fields,
                      levels = df_wos$Fields[order(df_wos$n_art,decreasing = TRUE)])

ggplot(df_wos) +
  aes(x = Fields, y = n_art, alpha = n_art) +
  geom_col(fill = "dodgerblue4")+
  scale_fill_hue() +
  labs(x=NULL, y = "Number of papers") +
  theme_bw()+
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))

If you want to map to a colour scale gradient, you just set the fill to the y-value and specify the correct colour scale.

ggplot(df_wos) +
  aes(x = Fields, y = n_art, fill = n_art) +
  geom_col()+
  scale_fill_gradient(low = "transparent", high = "dodgerblue4") +
  labs(x=NULL, y = "Number of papers") +
  theme_bw()+
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))

Lastly, if you want a top-to-bottom gradient within the bars, you can use the ggpattern package.

library(ggpattern)
#> 
#> Attaching package: 'ggpattern'
#> The following objects are masked from 'package:ggplot2':
#> 
#>     flip_data, flipped_names, gg_dep, has_flipped_aes, remove_missing,
#>     should_stop, waiver

ggplot(df_wos) +
  aes(x = Fields, y = n_art) +
  geom_col_pattern(
    colour = NA,
    fill = "dodgerblue4",
    pattern = "gradient",
    pattern_fill = "transparent"
  )+
  scale_fill_hue() +
  labs(x=NULL, y = "Number of papers") +
  theme_bw()+
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))

Created on 2021-03-01 by the reprex package (v1.0.0)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.