在y轴上更改值的顺序

时间:2019-05-20 19:05:43

标签: r ggplot2

我用ggplot2制作了直方图。但是列的高度不遵循y轴上值的比例。另外,y轴上的值顺序也不正确。

#This is the code I used.
data1 <- matrix(c("1", "2", "3", "4304", "456", "30"), nrow = 3, ncol = 2, dimnames = list(1:3, c("number_gRNA", "ncell")))

ggplot(data1, aes(number_gRNA, ncell)) + geom_col(width = 0.4, fill="#56B4E9", colour="black") + ggtitle("sgRNA distribution")

enter image description here

我希望列的高度可以跟随其值的比例。同时,y轴上的值遵循递增顺序。

1 个答案:

答案 0 :(得分:3)

好的,我做了2个简单的更改以使其正常工作。

首先,ggplot()不会采用矩阵,因此我使用as_tibble()使其起作用。

但是对于您的问题,它使用number_gRNA作为字符,并假设它是绝对的。因此,您需要as.numeric()才能实现。

library(tidyverse)

data1 <- matrix(
  c("1", "2", "3", "4304", "456", "30"),
  nrow = 3,
  ncol = 2,
  dimnames = list(1:3, c("number_gRNA", "ncell"))
)
data1 %>% 
  as_tibble() %>% 
  mutate(ncell = as.numeric(ncell)) %>% 
  ggplot(aes(number_gRNA, ncell)) +
  geom_col(width = 0.4, fill = "#56B4E9", colour = "black") +
  ggtitle("sgRNA distribution")
相关问题