简体   繁体   English

在ggplot2上使用百分比刻度时如何设置间隔?

[英]How do I set breaks when using a percentage scale on ggplot2?

I'm trying to manually enter when the breaks on the axis labels should be for my graph -- ie every 10 percentage points, every 25, etc, instead of having ggplot calculate them automatically. 我试图手动输入何时轴标签上的中断应该适合我的图形-即每10个百分点,每25个百分点等,而不是让ggplot自动计算它们。

ggplot(cars, aes(x=speed)) +
  geom_histogram(binwidth=.1, colour="white", fill="dark green", aes(y = (..count..)/sum(..count..))) + 
scale_x_continuous(labels=percent) +
scale_y_continuous(labels=percent) 

This is sort of random data, but shows the basic idea -- I would like to be able to set these in the: 这是一种随机数据,但显示了基本思想-我希望能够在以下位置进行设置:

scale_x_continuous(labels=percent)

in the same way as one can write that there should be breaks every x units using this code: 就像可以写成这样,使用此代码每个x单位都应有中断:

scale_x_continuous(breaks=seq(0,2, by=.1))

Does anyone have suggestions? 有人有建议吗?

The breaks argument and the labels argument are not exclusive. breaks参数和labels参数不是唯一的。 The former is for determining where the breaks should be and the latter is for how they should be formatted/displayed. 前者用于确定换行符应位于何处,而后者用于确定换行符的格式/显示方式。 Thus you can do 这样你就可以做

scale_x_continuous(breaks = seq(0, 2, by = 0.1), labels = percent)

to specify both aspects. 同时说明这两个方面。

Something like: 就像是:

ggplot(cars, aes(x=speed)) +
  geom_histogram(binwidth=.1, colour="white", fill="dark green", aes(y = (..count..)/sum(..count..))) + 
  scale_x_continuous(breaks= seq(0,1,.1)*max(cars$speed), labels = seq(0,100,10)) + labs(x = "speed (%)")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ggplot2 的 plot_usmap 时,如何将比例设置为静态窃取动态 - How do I set the scale to be static insteal of dynamic when using ggplot2's plot_usmap 使用 scale_x_log10 时,如何在 geom_histogram 中设置 ggplot2 binwidth? - How do I set ggplot2 binwidth in geom_histogram when using scale_x_log10? 如何使用ggplot2在时间间隔上缩放轴? - How do I scale an axis for time intervals using ggplot2? 使用对数刻度时,如何在ggplot2中设置轴范围? - How can I set axis ranges in ggplot2 when using a log scale? 当我在 R ggplot2 中绘制缺失的日期时,我如何调整 X 轴的比例并将中断设为整数 - How I can adjust the scale of axis X with breaks as.integer when I graph missing dates in R ggplot2 使用R中的ggplot2进行时序图时,如何在x轴上正确缩放日期? - How do I scale dates correctly on x-axys when doing time-series graph using ggplot2 in R? 如何在ggplot2的geom_line中显示百分比? - How do I display percentage in geom_line in ggplot2? 如何在ggplot2中调整geom_tile的比例? - How do I adjust the scale of a geom_tile in ggplot2? 当我使用 plot 连续颜色使用 ggplot2 时,如何修复图例中缺少的色标? - How can I fix missing color scale in the legend when I plot continuous color using ggplot2? 在 ggplot2 中使用 scale_color_manual 时,中断和值在图中不匹配 - breaks and values not matching up in plot when using scale_color_manual in ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM