Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot gradient fill not working

Tags:

r

ggplot2

I have the below data set and r code to generate the ggplot.

   df = data.frame(Animals = c("Dog", "Cat", "Horse", "Giraffe"), Number =           c(88, 11, 107, 59),
            Cat = c("A", "A", "B", "B"),
            Place=c("Place1","Place2"))
 ggplot(df, aes(Animals, Cat, fill=Number)) + 
 geom_tile() +
 scale_fill_gradient2(low= "red", high = "green",
                   mid = "orange",
                   midpoint = 50,
                   space = "Lab")+
geom_text(label=df$Number)+
facet_grid(.~Place)

output of the above code is,

enter image description here

if you see the graph, the gradient fill is not as mentioned in the code. as per the code the higher number should be in green.

Need some expert view on this.

like image 471
vanathaiyan Avatar asked Mar 01 '17 06:03

vanathaiyan


People also ask

How do I fix ggplot2 that doesn't work?

If the previous fixes don’t work, you may need to remove the current version of ggplot2 completely and re-install it: #remove ggplot2 remove.packages ("ggplot2") #install ggplot2 install.packages ("ggplot2") #load ggplot2 library(ggplot2) #create scatterplot of x vs. y ggplot (df, aes (x=x, y=y)) + geom_point ()

How to use gradient2 in ggplot2?

# For diverging colour scales use gradient2 ggplot (df, aes (x, y)) + geom_point (aes (colour = z1)) + scale_colour_gradient2 () # Use your own colour scale with gradientn ggplot (df, aes (x, y)) + geom_point (aes (colour = z1)) + scale_colour_gradientn (colours = terrain.colors (10))

What does “could not find function “ggplot”” mean?

Error in ggplot(df, aes(x = x, y = y)) : could not find function "ggplot" This error occurs when you attempt to create a plot using the ggplot2 data visualization package, but have failed to load the package first. This tutorial explains five potential ways to fix this error. How to Reproduce this Error. Suppose we run the following code in R:

Why is my ggplot showing as blank?

This work-around succeeds in addressing a bug where certain fill values aren't respected and show up as blank for plotted objects: tidyverse/ggplot2#384 That sounds like a reasonable explanation. It has something to do with those lines changed in the commit, so it shouldn't be too hard to find and fix.


1 Answers

The gradient fill is correct. It's the text values that are incorrect. Note, for example, in the data below that Horse and B have a value of 107, but the text value for that tile in your plot is 11.

  Animals Number Cat  Place
1     Dog     88   A Place1
2     Cat     11   A Place2
3   Horse    107   B Place1
4 Giraffe     59   B Place2

Change to geom_text(aes(label=Number)). By using geom_text(label=df$Number), you're reaching outside the ggplot environment to the global environment version of df, in which the values of Number are (as ordered in the data frame), 88, 11, 107, 59. So that's the order in which the numbers are plotted (from left to right) in the graph. However, by mapping Number to label, by using aes(label=Number), you ensure that the label aesthetic and the fill aesthetic are both mapped to the graph in a corresponding way as determined by the "natural" logic of ggplot geoms and aesthetic mappings.

ggplot(df, aes(Animals, Cat, fill=Number)) + 
  geom_tile() +
  scale_fill_gradient2(low= "red", high = "green", mid = "orange",
                       midpoint = 50, space = "Lab")+
  geom_text(aes(label=Number)) +
  facet_grid(.~Place)

enter image description here

This appears to be a toy example, but I thought I'd also point out that you can have ggplot remove space for empty values by using scales="free_x" when you facet:

ggplot(df, aes(Animals, Cat, fill=Number)) + 
  geom_tile() +
  scale_fill_gradient2(low= "red", high = "green", mid = "orange",
                       midpoint = 50, space = "Lab")+
  geom_text(aes(label=Number)) +
  facet_grid(.~Place, scales="free_x")

enter image description here

like image 93
eipi10 Avatar answered Oct 23 '22 13:10

eipi10