带有数据标签的多条线(R)

发布于 2025-02-09 21:26:15 字数 672 浏览 0 评论 0原文

我想用数据标签绘制多行 ,但是到目前为止,我所有的尝试都返回了一条错误消息。

ggplot(data, aes(year)) + 
  geom_line(aes(y = var1), color = "#E20335", size = 1.5) +
  geom_point(aes(y = var1), color = "#E20335", size = 3) +
  geom_line(aes(y = var2), color = "#292753", size = 1.5) +
  geom_point(aes(y = var2), color = "#292753", size = 3) +
  scale_x_continuous(breaks = 2010:2020) +
  scale_y_comma() +
        labs(title = 'something',
         subtitle = 'something',
         x = 'This axis title intentionally left blank',
         y = 'This axis title intentionally left blank',
         caption = 'something')

我想将标签插入此代码中,以便将它们放置在数据点周围。任何帮助将不胜感激。

I would like to plot multiple lines with data labels but all my attempts have returned an error message so far.

ggplot(data, aes(year)) + 
  geom_line(aes(y = var1), color = "#E20335", size = 1.5) +
  geom_point(aes(y = var1), color = "#E20335", size = 3) +
  geom_line(aes(y = var2), color = "#292753", size = 1.5) +
  geom_point(aes(y = var2), color = "#292753", size = 3) +
  scale_x_continuous(breaks = 2010:2020) +
  scale_y_comma() +
        labs(title = 'something',
         subtitle = 'something',
         x = 'This axis title intentionally left blank',
         y = 'This axis title intentionally left blank',
         caption = 'something')

I want to insert the labels into this code, so that they are placed around data points. Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

皇甫轩 2025-02-16 21:26:15

如评论中所建议的,您可以尝试使用geom_label。但是,如果您提供一个示例数据集,例如内置数据集或dput(data)的输出,则更好。另外,包括library您正在与您一起帮助人们帮助您。由于您没有指定要标记哪个变量,因此我

library(ggplot2)
library(hrbrthemes)
ggplot(mtcars, aes(mpg)) + 
  geom_line(aes(y = disp), color = "#E20335", size = 1.5) +
  geom_point(aes(y = disp), color = "#E20335", size = 3) +
  geom_line(aes(y = hp), color = "#292753", size = 1.5) +
  geom_point(aes(y = hp), color = "#292753", size = 3) +
  scale_x_continuous(breaks = 2010:2020) +
  scale_y_comma() +
  labs(title = 'something',
       subtitle = 'something',
       x = 'This axis title intentionally left blank',
       y = 'This axis title intentionally left blank',
       caption = 'something')+
  geom_label(aes(y=disp,label=carb))+
  geom_label(aes(y=hp,label=carb))

在此图和数据中发布了一个示例,其中包括mtcars数据集和数据,通常最好将数据集重新设计为长格式,以便更容易要管理传奇,颜色和其他ggplot选项 ”在此处输入图像描述”

As suggested in the comment you can try with geom_label. However, it is better if you provide an example dataset such as an in-built dataset or the output of dput(data). Also, include the library you are working with to help people help you. Since you do not specify which variable do you want to label I post an example with the mtcars dataset

library(ggplot2)
library(hrbrthemes)
ggplot(mtcars, aes(mpg)) + 
  geom_line(aes(y = disp), color = "#E20335", size = 1.5) +
  geom_point(aes(y = disp), color = "#E20335", size = 3) +
  geom_line(aes(y = hp), color = "#292753", size = 1.5) +
  geom_point(aes(y = hp), color = "#292753", size = 3) +
  scale_x_continuous(breaks = 2010:2020) +
  scale_y_comma() +
  labs(title = 'something',
       subtitle = 'something',
       x = 'This axis title intentionally left blank',
       y = 'This axis title intentionally left blank',
       caption = 'something')+
  geom_label(aes(y=disp,label=carb))+
  geom_label(aes(y=hp,label=carb))

For this kind of plot and data it is typically better to reshape the dataset to a long format so it is easier to manage legend, color and other ggplot optionsenter image description here

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