使用scale_x_discrete或scale_x_continuule时找不到r对象

发布于 2025-01-30 19:15:12 字数 1213 浏览 4 评论 0原文

hour_mark   avg_intensity
<int>   <dbl>
0   2.1295503
1   1.4190782
2   1.0439443
3   0.4437299
4   0.6330472
5   4.9506438
6   7.7712137
7   10.7336198
8   14.6680988
9   15.3877551
10  17.6437029
11  16.9212513
12  19.8470716
13  18.7752443
14  18.8686211
15  15.5846995
16  17.7166483
17  21.6556291
18  21.9216336
19  21.3852097
20  14.3399558
21  12.0729282
22  9.0630531
23  4.9966777
ggplot(data=avg_int_hourly,
    aes(x=hour_mark,
        y=avg_intensity,group=1))+
    geom_line(color="red")+
    geom_point()+
    scale_x_discrete(labels=hour_mark)+
    labs(title='Average Intensity Each Hour of the Day')+
    xlab('Hours of Day')+
    ylab('Average Intensity')

我想在X轴上代表所有24小时的时间,因此我尝试使用定义的X(Hour_mark)尝试了Scale_x_discrete和Scale_x_continuul,但R找不到该对象:

Error in check_breaks_labels(breaks, labels): object 'hour_mark' not found
Traceback:

1. scale_x_discrete(labels = hour_mark)
2. discrete_scale(c("x", "xmin", "xmax", "xend"), "position_d", 
 .     identity, ..., expand = expand, guide = guide, position = position, 
 .     super = ScaleDiscretePosition)
3. check_breaks_labels(breaks, labels)

请帮助!

hour_mark   avg_intensity
<int>   <dbl>
0   2.1295503
1   1.4190782
2   1.0439443
3   0.4437299
4   0.6330472
5   4.9506438
6   7.7712137
7   10.7336198
8   14.6680988
9   15.3877551
10  17.6437029
11  16.9212513
12  19.8470716
13  18.7752443
14  18.8686211
15  15.5846995
16  17.7166483
17  21.6556291
18  21.9216336
19  21.3852097
20  14.3399558
21  12.0729282
22  9.0630531
23  4.9966777
ggplot(data=avg_int_hourly,
    aes(x=hour_mark,
        y=avg_intensity,group=1))+
    geom_line(color="red")+
    geom_point()+
    scale_x_discrete(labels=hour_mark)+
    labs(title='Average Intensity Each Hour of the Day')+
    xlab('Hours of Day')+
    ylab('Average Intensity')

I would like to represent all 24 hours of day in the x-axis so I tried scale_x_discrete and scale_x_continuous using the defined x (hour_mark) but R can't find the object:

Error in check_breaks_labels(breaks, labels): object 'hour_mark' not found
Traceback:

1. scale_x_discrete(labels = hour_mark)
2. discrete_scale(c("x", "xmin", "xmax", "xend"), "position_d", 
 .     identity, ..., expand = expand, guide = guide, position = position, 
 .     super = ScaleDiscretePosition)
3. check_breaks_labels(breaks, labels)

Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2025-02-06 19:15:12

两件事:

  • scale_x_discrete需要一个离散的值,您已经提供了一个连续的数字列。我们可以使用factor来强制此操作。 (请注意,虽然下面有效,但如果未以数字方式订购数据,那么我们呼叫factor可能需要使用factor(。,Levels =)< /code>。)


  • 比例_*函数不进行非标准评估与geom _*函数相同,需要传递显式向量/对象。从技术上讲,我们在这里不需要这个,但是我将包括一个可以做更多的控制(评论)。

ggplot(data=avg_int_hourly,
    aes(x=factor(hour_mark),
        y=avg_intensity,group=1))+
    geom_line(color="red")+
    geom_point()+
    ## don't really need this, since the default action for discrete is
    ## to show all of them on the axis
    # scale_x_discrete(breaks=avg_int_hourly$hour_mark, labels=avg_int_hourly$hour_mark)+
    labs(title='Average Intensity Each Hour of the Day')+
    xlab('Hours of Day')+
    ylab('Average Intensity')

Two things:

  • scale_x_discrete needs a discrete value, you've provided a continuous one with a numeric column. We can force this by using factor. (Note that while this works below, if the data is not ordered in a number-like fashion, then our call to factor might need to be explicit with factor(., levels=).)

  • scale_* functions do not do non-standard evaluation the same as the geom_* functions, one needs to pass explicit vectors/objects. Technically we don't need this here, but I'll include what one could do for more control (commented out).

ggplot(data=avg_int_hourly,
    aes(x=factor(hour_mark),
        y=avg_intensity,group=1))+
    geom_line(color="red")+
    geom_point()+
    ## don't really need this, since the default action for discrete is
    ## to show all of them on the axis
    # scale_x_discrete(breaks=avg_int_hourly$hour_mark, labels=avg_int_hourly$hour_mark)+
    labs(title='Average Intensity Each Hour of the Day')+
    xlab('Hours of Day')+
    ylab('Average Intensity')

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文