以下代码来自Creating a horizontal bar plots in the reverse direction。
我想反向制作水平条形图,但是固定比例。我希望将绘图的比例固定为 0到50 ,而不是当前的0到30.
我已尝试将scale_y_continuous()
更改为scale_y_discret()
,我尝试添加ylim()
,但似乎无法让它发挥作用。
谢谢!
mtcars
mtcars$`car name` <- rownames(mtcars)
ggplot (mtcars, aes (x=`car name`, y=-mpg)) +
geom_bar (position = position_dodge(), stat = "identity",fill="red",colour="black") +
coord_flip () + theme_classic() +
scale_x_discrete(name = "", position = "top") +
scale_y_continuous(name = "mpg",
breaks = seq(0, -30, by = -10),
labels = seq(0, 30, by = 10)) + theme(axis.text.y = element_blank())
答案 0 :(得分:-1)
您可以使用limits设置y比例的限制,它是一个长度为2的数字向量,用于描述比例限制。
正如@jimbou在评论中提到的那样,你的代码将是这样的:ggplot (mtcars, aes (x=`car name`, y=-mpg)) +
geom_bar (position = position_dodge(), stat = "identity",fill="red",colour="black") +
coord_flip () + theme_classic() +
scale_x_discrete(name = "", position = "top") +
scale_y_continuous(name = "mpg",
limits = c(-50,0),
breaks = seq(0, -50, by = -10),
labels = seq(0, 50, by = 10)) + theme(axis.text.y = element_blank())
您还可以看到here