Ggplot gradient not working

I have the following dataset and r code to generate 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,

enter image description here

If you see a graph, the gradient fill is not mentioned in the code. according to the code, the larger number should be green.

You need some kind of expert opinion on this.

+4
source share
1 answer

The gradient fills correctly. These are incorrect text values. Note, for example, in the data below that Horsethey Bhave a value of 107, but the text value for this fragment in your chart 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

geom_text(aes(label=Number)). geom_text(label=df$Number), ggplot df, Number ( ), 88, 11, 107, 59. , , ( ) . , Number label, aes(label=Number), , label fill , "" ggplot .

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

, , , ggplot , scales="free_x", :

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

+5

Source: https://habr.com/ru/post/1671105/


All Articles