下面的代码来自 Creating a horizontal bar plots in the reverse direction .
我想做一个反向的水平条形图,但是用 固定比例 . 我想把情节的比例定在 0到50 而不是当前的0到30。
我试过改变 scale_y_continuous() 到 scale_y_discret() 我试着补充 ylim() 但我似乎不能让它工作。
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())
你可以用 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