Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change ylims on dotplot with ggplot

I want to build a dot plot with y limits from -3 to 3. This one time, my data does not contain -3, so it does not show up on my plot. I was not abe to manually change ylim to (-3,3). Is there a way to do this? I tried ylim=c(-3,3) and scale_y_discrete(-3,3)

Here is the ggplot code I used to create my dot plot. It works, but the limits on y are -2 and 3.

graf.dot <- ggplot(df.dot,aes(x = data, y = nota, group = data, fill = colour, colour = colour)) + facet_wrap(~taxa) + 
            geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 2) + 
            scale_fill_manual(values = c('Negativo' = 'red','Neutro' = 'gray','Positivo'='green','Ref' = 'black')) + 
            scale_colour_manual(values = c('Negativo' = 'red','Neutro' = 'gray','Positivo'='green','Ref' = 'black')) + 
            ggtitle("Notas \n") +  xlab("") + ylab("") + theme(plot.title = element_text(size = 20, hjust = 0.5), 
            axis.text.y = element_text(size = 18), axis.text.x = element_text(size = 18),legend.key = element_blank(), legend.title = element_blank(), legend.text = element_text(size = 18), legend.position = "bottom", 
            strip.text = element_text(size = 18)); 

And some sample data for my problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df.dot=structure(list(taxa = c("T1", "T1", "T1", "T1", "T1", "T1", "T1", 
"T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", 
"T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", 
"T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", 
"T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", 
"T2", "T2", "T2", "T2", "T2", "T2", "T2", "T3", "T3", "T3", "T3", 
"T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", 
"T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", 
"T3", "T3", "T3", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", 
"T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", 
"T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T5", 
"T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", 
"T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", 
"T5", "T5", "T5", "T5", "T5", "T5", "T6", "T6", "T6", "T6", "T6", 
"T6", "T6", "T6", "T6", "T6", "T6", "T6", "T6", "T6", "T6", "T6", 
"T6", "T6", "T6", "T6", "T6", "T6", "T6", "T6", "T6", "T6", "T6", 
"T6", "T6", "T7", "T7", "T7", "T7", "T7", "T7", "T7", "T7", "T7", 
"T7", "T7", "T7", "T7", "T7", "T7", "T7", "T7", "T7", "T7", "T7", 
"T7", "T7", "T7", "T7", "T7", "T7", "T7", "T7", "T7"), data = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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2L), .Label = c("Negativo", 
"Neutro", "Positivo", "Ref"), class = "factor")), row.names = c(NA, 
-203L), class = "data.frame")

>Solution :

You should add breaks and limits to scale_y_discrete like this:

library(ggplot2)
graf.dot <- ggplot(df.dot,aes(x = data, y = nota, group = data, fill = colour, colour = colour)) + facet_wrap(~taxa) + 
  geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 2) + 
  scale_fill_manual(values = c('Negativo' = 'red','Neutro' = 'gray','Positivo'='green','Ref' = 'black')) + 
  scale_colour_manual(values = c('Negativo' = 'red','Neutro' = 'gray','Positivo'='green','Ref' = 'black')) + 
  scale_y_discrete(breaks = factor(c(-3:3)), limits = c("-3", "-2", "-1", "0", "1", "2", "3")) +
  ggtitle("Notas \n") +  xlab("") + ylab("") + theme(plot.title = element_text(size = 20, hjust = 0.5), 
                                                     axis.text.y = element_text(size = 18), axis.text.x = element_text(size = 18),legend.key = element_blank(), legend.title = element_blank(), legend.text = element_text(size = 18), legend.position = "bottom", 
                                                     strip.text = element_text(size = 18)); 

graf.dot
#> Bin width defaults to 1/30 of the range of the data. Pick better value with
#> `binwidth`.

Created on 2023-01-04 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading