如何使用字符串值扩展X轴并在GGPLOT2中绘制矩形? r
我想在X轴上添加两个值(1和5000),并使用类似的内容绘制矩形:
geom_rect(aes(xmin = 1,
xmax = 5000,
ymin = -Inf, ymax = Inf))
有没有办法将X轴值作为字符串保持?
A tibble: 11 x 5
trainingSet testOn mean lci uci
<chr> <chr> <dbl> <dbl> <dbl>
1 1 to 5000 10000 0.930 0.927 0.934
2 1 to 5000 15000 0.932 0.930 0.935
3 1 to 5000 20000 0.932 0.929 0.936
4 1 to 5000 25000 0.935 0.931 0.938
5 1 to 5000 30000 0.934 0.930 0.939
6 1 to 5000 35000 0.488 0.486 0.490
7 1 to 5000 40000 0.498 0.496 0.500
8 1 to 5000 45000 0.489 0.487 0.491
9 1 to 5000 50000 0.484 0.481 0.487
10 1 to 5000 55000 0.493 0.490 0.496
11 1 to 5000 60000 0.481 0.478 0.484
源代码:
ggplot(data = confidence.intervals, aes(y = mean, x = testOn, color=trainingSet))+
geom_ribbon(aes(x= testOn, ymin=lci, ymax=uci, group=trainingSet, fill=trainingSet), alpha = 0.1, show.legend = FALSE)+
geom_line(aes(group=1))+
geom_point(size = 1.5)
预先感谢您!
I would like to add two more values (1 and 5000) on the x-axis and draw a rectangle using something like:
geom_rect(aes(xmin = 1,
xmax = 5000,
ymin = -Inf, ymax = Inf))
Is there a way to do it keeping the x-axis values as strings?
A tibble: 11 x 5
trainingSet testOn mean lci uci
<chr> <chr> <dbl> <dbl> <dbl>
1 1 to 5000 10000 0.930 0.927 0.934
2 1 to 5000 15000 0.932 0.930 0.935
3 1 to 5000 20000 0.932 0.929 0.936
4 1 to 5000 25000 0.935 0.931 0.938
5 1 to 5000 30000 0.934 0.930 0.939
6 1 to 5000 35000 0.488 0.486 0.490
7 1 to 5000 40000 0.498 0.496 0.500
8 1 to 5000 45000 0.489 0.487 0.491
9 1 to 5000 50000 0.484 0.481 0.487
10 1 to 5000 55000 0.493 0.490 0.496
11 1 to 5000 60000 0.481 0.478 0.484
Source code:
ggplot(data = confidence.intervals, aes(y = mean, x = testOn, color=trainingSet))+
geom_ribbon(aes(x= testOn, ymin=lci, ymax=uci, group=trainingSet, fill=trainingSet), alpha = 0.1, show.legend = FALSE)+
geom_line(aes(group=1))+
geom_point(size = 1.5)
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的teston列是一个字符列,因此我们必须
add_row()
“ 0”和“ 5000”的两个行(由于图中的X轴将离散)。然后,您可以通过更改为factor并使用Forcats使用fct_relevel
将它们移至正面。代码
输出
data
Since your testOn column is a character column, we have to
add_row()
two rows for "0" and "5000" (since the x axis in your plot becomes discrete). Then you can move them to the front by changing to factor and usingfct_relevel
from forcats.Code
Output
Data