Order Data Inside a Geom_Tile

Order data inside a geom_tile

I usually try to use levels to fix my data before hand:

x <- structure(list(V1 = c("a", "y", "w", "p", "v", "h", "i"), 
V2 = c("r", "w", "q", "m", "l", "q", "g"), V3 = c(
"5", "2", "9", "2", "1", "3", "0")), .Names = c("V1", "V2",
"V3"), class = "data.frame", row.names = c(NA, -8L))

x <- x[1:7,]

x$V1 <- factor(x$V1, levels=(x$V1)[order(x$V3)])

# Note it's not an ordered factor, it's a factor in the right order

ggplot(x, aes(V1, V3)) + geom_tile()

Sample Image

UPDATE:

Still not exactly clear on your dimensions, but perhaps something like:

x$V2 <- factor(x$V2, levels=(x$V2)[order(x$V3)])

ggplot(x, aes(V1,V2,fill=V3)) + geom_tile()

Sample Image

sorting data in geom_tile()

Using the data from your other question:

library(dplyr); library(forcats) # please check these load successfully
GFP_long %>%
arrange(desc(variable), -value) %>%
mutate(Entrez.Symbol = fct_inorder(Entrez.Symbol)) %>%
ggplot(aes(x=Entrez.Symbol,y=variable,fill=value)) +
geom_tile(aes(fill = value))+
# I'm modifying here to show the top row is ordered; hard to see in original
scale_fill_gradient2(low="red", mid = "gray80", high="green", midpoint = 0.2)

Sample Image

Data: how to order column by a different column value r

GFP_long<-data.frame(Entrez.Symbol<-c("TRIP11","SIN3B","SF3B1","PSMD14","RAD51","BARD1", 
"BRCA1","BRCA1","U5200KD","SETD5","TRIP11","SIN3B",
"SF3B1","PSMD14","RAD51","BARD1","BRCA1","BRCA1",
"U5200KD","SETD5"),
variable<-c("GFP_my","GFP_my","GFP_my","GFP_my","GFP_my","GFP_my","GFP_my",
"GFP_my","GFP_my","GFP_my","GFP_wide","GFP_wide","GFP_wide","GFP_wide",
"GFP_wide","GFP_wide","GFP_wide","GFP_wide","GFP_wide","GFP_wide"),
value<-c(1.015,0.336,0.315,0.254,0.286,0.157,0.275,0.230,0.772,0.364,
0.020,0.055,0.071,0.102,0.109,0.139,0.146,0.146,0.151,0.179))

colnames(GFP_long)<-c("Entrez.Symbol","variable","value")

How to order of tiles in geom_tile ggplot by using only part of the numeric variables

Something like this? Just create a static variable that manages the colour gradient for ya.

library(tidyverse)
levels <- c("none","I30","I300","I30_V10","I300_V100","V10","V100" )

# Data directory %>%
read_csv %>%
pivot_wider(names_from = Combination,
values_from = Avg.percent) %>%
mutate(color = none) %>%
pivot_longer(cols = c("none", starts_with(c(c("I","V"), ignore.case = F))),
names_to = "Combination",
values_to = "Avg.percent") %>%
mutate(Combination = factor(Combination,
levels = levels))-> data

m <- ggplot(data)+
geom_tile(aes(x=Combination, y=reorder(Drug.dose, color), fill=Avg.percent)) +
geom_text(aes(x=Combination, y=reorder(Drug.dose, color), label=Avg.percent), size=3)+
# scale_fill_gradientn(colors=pal)+
ggsci::scale_fill_material("red") +
theme(legend.text = element_text(size=10, face="bold", color = "black"))+
theme(axis.text.x = element_text(size = 15, face="bold", color = "black")) +
theme(axis.text.y = element_text(size = 9, face="bold", color = "black")) +
theme(axis.title.x = element_text(size = 15, face="bold", color = "black", vjust = 3))+
theme(axis.title.y = element_text(size = 15, face="bold", color = "black", hjust = 0.5))+
theme(plot.title = element_text(size = 16))+
theme(strip.text.y = element_text(size = 10, face = "bold", color = "black"))+
scale_x_discrete(position ="top") +
xlab("Combination added")+
ylab("Treatments in the screen")+
ggtitle("Cluster 1 Enriched in TN response")

print(m)

Order data for ggplot2 geom_tile

In order to keep your factors ordered alphabetically within the Dog/Cat groups you will need to order the levels based on both the variables.

tr$IND1 <- factor(tr$IND1, levels = unique(tr$IND1[order(tr$IND1.TYPE, tr$IND1)]))
tr$IND2 <- factor(tr$IND2, levels = unique(tr$IND2[order(tr$IND2.TYPE, tr$IND2)]))

Then your plot axes will have the desired order.

Sample Image

Order of columns and rows with ggplot2 tile

Here's an answer you could try. I have refactored things outside of ggplot. This is a personal preference, as I like to be able to check things and I've made many mistakes with it in the past.

#set vector of levels you want
mylevels <- tableau$Name

#reorder factors
tableau.m$Name <- factor(tableau.m$Name,levels=mylevels)
tableau.m$variable <- factor(tableau.m$variable, levels=mylevels)

#plot
ggplot(tableau.m, aes(variable, Name)) +
geom_tile(aes(fill = value), colour = "white") +
scale_fill_distiller(palette = "Spectral") +
theme(axis.text.x = element_text(angle = 270))

Sample Image

geom_tile() in R: Sort Order

As @Nate already mentioned you have to convert your vars to factors and put the levels in the right order. Instead of converting via factor(as.numeric(.)) (which converts "10 or more" to NA) I would recommend to make use of forcats::fct_relevel which allows you to change the order of the levels, e.g. forcats::fct_relevel(homeScore, "10 or more", after = 10) will change the order of the levels such that 10 or more becomes the last level. Try this:

library(ggplot2)
library(tidyr)
library(dplyr)
library(forcats)

df %>%
as_tibble(rownames = "homeScore") %>%
pivot_longer(cols = -homeScore, names_to = "awayScore", values_to = "level") %>%
mutate_at(vars(homeScore, awayScore), ~forcats::fct_relevel(.x, "10 or more", after = 10)) %>%
ggplot() +
geom_tile(aes(x=homeScore, y=awayScore, fill = level))

Sample Image

Problem maintaing order with geom_tile and duplicated data

Reorder your variables, see:

tableau.m$Team <- factor(tableau.m$Team, c("Brasil", "Argentina", "Uruguay"))
tableau.m$variable <- factor(tableau.m$variable, c("Titles", "Match", "Points", "Points_1", "Performance"))

ggplot(tableau.m, aes(variable, Team, fill = rescale)) +
geom_tile(show.legend = FALSE) +
scale_fill_gradient(low = "white", high = "steelblue") +
theme_minimal()

Sample Image

Reorder rows according to two column values in geom_tile() heatmap

You can use fct_relevel from the package forcats, if this is what you are looking for.

library(tidyverse)

library(forcats)
ro <- c('Japan', 'Germany', 'Norway', 'USA','Sweden', 'UK')

df %>%
mutate(country= fct_relevel(country, ro)) %>%
ggplot(aes(year, country, fill = factor(value))) +
geom_tile(color = "white", size = 1)+
theme_minimal()

Sample Image



Related Topics



Leave a reply



Submit