R语言 使用ggplot更改点图上的ylim

z4iuyo4d  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(135)

我想构建一个y限制为-3至3的点图。这一次,我的数据不包含-3,因此它没有显示在我的图上。我不想手动将ylim更改为(-3,3)。有办法吗?我尝试了ylim=c(-3,3)scale_y_discrete(-3,3)
这是我用来创建点图的ggplot代码,它可以工作,但是y的限制是-2和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));

和一些样本数据来解决我的问题。

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, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
1L, 1L, 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L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 2L, 3L), .Label = c("-2", "-1", 
"0", "1", "2", "3"), class = "factor"), colour = structure(c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Negativo", 
"Neutro", "Positivo", "Ref"), class = "factor")), row.names = c(NA, 
-203L), class = "data.frame")
qjp7pelc

qjp7pelc1#

应该将breakslimits添加到scale_y_discrete,如下所示:

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`.

创建于2023年1月4日,使用reprex v2.0.2

相关问题