Reputation: 1017
I have created a tile plot, as below:
I'd like to rename the y axis ticks, instead of 1 - to "country", 2- to "city", 3 - to "all". I couldn't find a way to do it. I have the following script:
ggplot(test_df, aes(num, sample_type, fill = exist)) +
geom_tile(width=0.9, height=0.9,color = "gray")+
scale_fill_gradientn(colours = c("white", "lightgreen"), values = c(0,1))+
theme(axis.text.x = element_text(angle = 90, hjust = 1),
legend.position="none")+
scale_x_discrete(position = "top") + labs(x = "", y= "Sample Type")
How can I do that without changing my dataframe?
Here is a sample of my dataframe:
test_df <- data.frame(
num = c("num1","num1","num1","num2","num2","num2","num3","num3","num3"),
sample_type = c(1,2,3,1,3,2,3,1,2),
exist = c(1,0,0,1,1,0,1,0,0))
Upvotes: 1
Views: 7623
Reputation: 206167
It looks like you want to treat your Y values as discreate values, rather than numeric. The easiest way to deal with that is to make the number a factor. You can do
ggplot(test_df, aes(num, factor(sample_type), fill = exist)) +
geom_tile(width=0.9, height=0.9,color = "gray")+
scale_fill_gradientn(colours = c("white", "lightgreen"), values = c(0,1))+
theme(axis.text.x = element_text(angle = 90, hjust = 1),
legend.position="none")+
scale_y_discrete(labels=c("1"="country","2"="city", "3"="all")) +
scale_x_discrete(position = "top") + labs(x = "", y= "Sample Type")
Notice the aes(num, factor(sample_type), ...)
to convert sample_type to a factor and then the scale_y_discrete
to control the labels for those levels.
Upvotes: 5