【问题标题】:Show a specific value of x-axis on ggplot在 ggplot 上显示 x 轴的特定值
【发布时间】:2014-09-16 04:57:23
【问题描述】:

我正在 x 轴上的特定位置创建带有 geom_vlineggplot。我希望 x 轴显示该特定值

以下是我的数据+代码:

dput(agg_data)
structure(list(latency = structure(c(0, 1, 2, 3, 4, 5, 6, 7, 
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 24, 26, 28, 
29, 32, 36, 37, 40, 43, 46, 47, 48, 49, 54, 64, 71, 72, 75, 87, 
88, 89, 93, 134, 151), class = "difftime", units = "days"), count = c(362, 
11, 8, 5, 4, 2, 8, 6, 4, 2, 2, 1, 5, 1, 2, 2, 2, 1, 1, 1, 2, 
1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 
1, 1, 1), cum_sum = c(362, 373, 381, 386, 390, 392, 400, 406, 
410, 412, 414, 415, 420, 421, 423, 425, 427, 428, 429, 430, 432, 
433, 435, 436, 437, 438, 439, 440, 441, 442, 444, 446, 447, 448, 
449, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460), cum_dist = c(0.78695652173913, 
0.810869565217391, 0.828260869565217, 0.839130434782609, 0.847826086956522, 
0.852173913043478, 0.869565217391304, 0.882608695652174, 0.891304347826087, 
0.895652173913044, 0.9, 0.902173913043478, 0.91304347826087, 
0.915217391304348, 0.919565217391304, 0.923913043478261, 0.928260869565217, 
0.930434782608696, 0.932608695652174, 0.934782608695652, 0.939130434782609, 
0.941304347826087, 0.945652173913043, 0.947826086956522, 0.95, 
0.952173913043478, 0.954347826086957, 0.956521739130435, 0.958695652173913, 
0.960869565217391, 0.965217391304348, 0.969565217391304, 0.971739130434783, 
0.973913043478261, 0.976086956521739, 0.980434782608696, 0.982608695652174, 
0.984782608695652, 0.98695652173913, 0.989130434782609, 0.991304347826087, 
0.993478260869565, 0.995652173913044, 0.997826086956522, 1)), .Names = c("latency", 
"count", "cum_sum", "cum_dist"), row.names = c(NA, -45L), class = "data.frame")

和代码:

library(ggplot2)
library(ggthemes)
    russ<-ggplot(data=agg_data,aes(x=as.numeric(latency),y=cum_dist))+geom_line(size=2)
    russ<-russ+ggtitle("Latency from first click to Qualified Demo:") + xlab("in Days")+ ylab("Proportion of maturity")+theme_economist()
    russ<-russ+geom_vline(aes(xintercept=10), color="black", linetype="dashed")
    russ

创建以下情节:

我希望绘图在 x 轴上显示值“10”(与 vline 相同的位置)

我寻找了一些其他类似的答案,比如Customize axis labels 但是这个重新创建了 x 轴标签(使用scale_x_discrete),并且不会在当前比例中添加新数字,这更多的是我正在寻找的。​​p>

提前致谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    在您的情况下,x 比例是连续的,因此您可以使用函数 scale_x_continuous() 并在您需要的位置提供休息。

    russ + scale_x_continuous(breaks=c(0,10,50,100,150))
    

    【讨论】:

    • 还有一件事:我是否必须查看绘图并查看当前的中断是什么才能添加 10,或者是否有一个函数可以提取当前的中断以便这个答案是创建到任何规模?
    • 使用此解决方案,您必须手动放置休息点。也许您可以添加opt this solution 将10 放在轴下方。
    • 谢谢,我尝试使用它:)
    猜你喜欢
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    相关资源
    最近更新 更多