在ggplot2中强制y轴为100%

5

I have a data frame that looks like this:

df <- data.frame(
group = c(rep("A", 4),rep("B", 4), rep("C", 4)),
word = c(rep(c("first", "first", "second", "second"), 3)), 
emphasis = c(rep(c("normal", "emphatic"), 6)), 
percentage = c(.175, .07, .13, .04, .60, .43, .21, .28, .63, .63, .40, .29))

我想创建一个类似于这个的折线图:

library(ggplot2)
p <- ggplot(df, aes(x = group, y = percentage, group = emphasis, col = emphasis))
p + geom_line() + facet_wrap(~ word)+ scale_y_continuous(label = percent) + geom_point(size=4, shape=21, colour="black") 

我无法完全弄清楚如何将 y 轴比例尺设置为 100%,因为没有一个数据点达到那么高。我以为 scale_y_continuous(limits = c(0, 100) 可以解决问题,但实际上不行。我想这一定很简单,但我找不到任何如何实现它的例子。


1
library(scales) 然后 scale_y_continuous(labels = percent_format(), limits=c(0,1)) - eipi10
谢谢,但对我没有用。(我忘记添加 library(scales) 了。) - JoeF
出了什么问题?当我运行您的代码并添加我的部分时,y轴上出现了百分比格式,并且限制为0%和100%。 - eipi10
抱歉,我在你的回答中漏掉了“限制”。谢谢。你想把它作为答案吗,这样我就可以标记它为正确的了? - JoeF
我已经将我的评论转化为答案。谢谢。 - eipi10
1个回答

10

这将为您提供从0%到100%的完整范围,带有百分比标签:

library(scales) # For the percent_format() function

scale_y_continuous(labels = percent_format(), limits=c(0,1))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接