Home > Back-end >  how to make the color in ggplot more detailed
how to make the color in ggplot more detailed

Time:02-26

I have a data like this

df <-structure(list(team_3_F = c("team ", "team ", "site", "site", 
"team ", "team ", "newyorkish", "newyorkish", "team ", "team ", 
"newyorkish", "newyorkish", "browingal ", "browingal ", "site", 
"site", "browingal ", "browingal ", "browingal ", "browingal ", 
"team ", "team ", "team ", "team ", "team ", "team ", "team ", 
"team ", "team ", "team ", "site", "site", "browingal ", "browingal ", 
"browingal ", "browingal ", "browingal ", "browingal ", "browingal ", 
"browingal ", "browingal ", "browingal ", "team ", "team ", "team ", 
"team ", "newyorkish", "newyorkish", "browingal ", "browingal ", 
"newyorkish", "newyorkish", "browingal ", "browingal ", "team ", 
"team ", "browingal ", "browingal ", "team "), name = c("AAA_US", 
"BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", 
"AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", 
"BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", 
"AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", 
"BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", 
"AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", 
"BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", 
"AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", 
"BBB_US", "AAA_US"), value = c(0L, 0L, 0L, 8L, 1L, 0L, 11L, 0L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 45L, 
0L, 0L, 0L, 18L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 3L, 
0L, 2L, 0L, 2L, 1L, 0L, 4L, 0L, 88L, 0L, 0L, 1L, 5L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 19L)), row.names = c(NA, -59L), class = "data.frame")

I plot it like this

ggplot(df, aes(x = team_3_F, y = name, fill = value))  
  geom_tile() 
  theme_classic(base_size = 12) 
  scale_fill_gradient2(low = "blue", high = "red",mid = "white",midpoint = 1)

I want to know if there is any other way to plot it so that I dont miss the small values because I have so many zero in the data

for instance, is there a possibility to plot as points instead of block of color ? for instance in the team , putting 6 points because 6 values exists for this category

if we look below

in team category , AAA_US has 6 values meaning 6 dots in team category , BBB_US has 0 values meaning 0 dots

team    AAA_US  0
team    AAA_US  1
team    AAA_US  0
team    AAA_US  0
team    AAA_US  45
team    AAA_US  0
team    AAA_US  18
team    AAA_US  0
team    AAA_US  1
team    AAA_US  4
team    AAA_US  0
team    AAA_US  19
team    BBB_US  0
team    BBB_US  0
team    BBB_US  0
team    BBB_US  0
team    BBB_US  0
team    BBB_US  0
team    BBB_US  0
team    BBB_US  0
team    BBB_US  0
team    BBB_US  0
team    BBB_US  0

CodePudding user response:

It sounds like you want finer-grained differentiation between colors in the lower part of the scale. You can do this using scale_fill_gradientn. However, note that you also have the problem that some tiles have the same x, y co-ordinates and are therefore being drawn over earlier tiles. My guess is that you should average the values to plot them properly. I would also suggest, given that a fine-grained legend is hard to read, that you add text values to represent the values at each tile:

ggplot(df, aes(x = team_3_F, y = name))  
    stat_summary_2d(
    aes(z = value, fill = after_stat(value)),
    fun = mean,
    geom = "tile"
  )  
  theme_classic(base_size = 12) 
  scale_fill_gradientn(colours = viridis::plasma(length(unique(df$value))),
                       values = sort(unique(df$value))/max(df$value))  
    stat_summary_2d(
    aes(z = value, 
        label = after_stat(scales::number(value, accuracy = 0.01))),
    fun = mean, color = "white", size = 8,
    geom = "text"
  )  
  coord_equal()

enter image description here


EDIT

If you want the color to reflect the number of non-zero counts, and want some dots to represent the non-zero counts instead of numbers, you can do:

ggplot(df, aes(x = team_3_F, y = name))  
    stat_summary_2d(
    aes(z = value, fill = after_stat(value)),
    fun = function(x) sum(x != 0),
    geom = "tile"
  )  
  theme_classic(base_size = 12)  
  geom_point(data = df[df$value != 0,], color = "white", fill = "black",
             shape = 21, size = 3,
             position = position_dodge2(width = 1))  
  scale_fill_gradient2(low = "blue3", mid = "white", high = "red3",
                       midpoint = 3.5)  
  coord_equal()

enter image description here

And if you want the numbers rather than the dots (since they look better IMO)

ggplot(df, aes(x = team_3_F, y = name))  
    stat_summary_2d(
    aes(z = value, fill = after_stat(value)),
    fun = function(x) sum(x != 0),
    geom = "tile"
  )  
  theme_classic(base_size = 12)  
  geom_label(data = df[df$value != 0,], color = "black", size = 8,
            stat = StatBin2d, aes(label = ..count..),
            fill = "white")  
  scale_fill_gradient2(low = "blue3", mid = "white", high = "red3",
                       midpoint = 3.5)  
  coord_equal()

enter image description here

  • Related