在条形图ggplot2中设置y限制 [英] Set y limits in Bar Chart ggplot2

查看:113
本文介绍了在条形图ggplot2中设置y限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个条形图,用于显示特定类别产品的销售情况.这是条形图.您可以看到它不是很清楚,所以我尝试设置Y轴的限制.

I have created a bar chart which shows the sales of products in a particular category. This is the bar chart. As you can see it is not very clear so I am trying to set limits for the Y axis.

我用以下行创建条形图:

I create the bar chart with the following line:

bakerySales <- ggplot(sales_bakery, aes(ProductName, ProductSales))+ 
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend = 
FALSE)

然后我继续使用以下方法将主题应用于条形图:

I then go on to apply a theme to the bar chart using:

bakerySales <- bakerySales +
theme(axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_text(colour = "black", size = 14, angle = 60, 
    hjust = 1),
    axis.text.y = element_text(colour = "black", size = 14),
    panel.background = element_rect(fill = "white"),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    axis.line = element_line(colour = "black", size = 1),
    legend.position = "none",
    plot.title = element_text(lineheight = 8, face = "bold"))

我尝试使用以下方法设置y轴的限制:

I have tried to set the limits for the y axis using:

bakerySales <- bakerySales + ylim(5000,10000)

执行此操作时,我会丢失条形图的内容,看起来像这样.

When I do this I lose the content of the bar chart, It looks like this.

有人可以告诉我我要去哪里了吗

Can someone please tell me where I am going wrong.

谢谢

推荐答案

如果要放大指定的ylimits,可以使用coord_cartesian函数.我没有面包店销售数据集,这是使用mtcars数据的示例:

If you want to zoom in on specifix ylimits, you could use the coord_cartesian function. I do not have the bakerysales dataset, this is an example using mtcars data:

ggplot(mtcars, aes(x = gear, y = qsec)) +
  stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend = FALSE) +
  coord_cartesian(ylim = c(200, 300))

这篇关于在条形图ggplot2中设置y限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆