GGPLOT 2中的着色线

发布于 2025-01-28 04:18:44 字数 6815 浏览 3 评论 0原文

我有下一个数据集。

  code2       clinical.pathway variable value
1  6002536 Primary Rehabilitation        1    19
2  6002536 Primary Rehabilitation        2    23
3  6002536 Primary Rehabilitation        3    22
4  6002536 Primary Rehabilitation        4    76
5  6002598 Primary Rehabilitation        1    16
6  6002598 Primary Rehabilitation        2    19
7  6002598 Primary Rehabilitation        3    51
8  6002598 Primary Rehabilitation        4    83
9  6002602           Hand Surgery        1    68
10 6002602           Hand Surgery        2    29
11 6002602           Hand Surgery        3    29
12 6002602           Hand Surgery        4    71
 .....

在临床途径中,我有更多因素,在这种情况下4。

我正在处理下一个情节,将其视为重复措施。

   ggplot(data)+
     geom_point(aes(x=variable,y=value,color=clinical.pathway),size=1.5,show.legend = F)+
     facet_wrap(~clinical.pathway)

我想以与点相同的颜色行加入每个点。 像这样:

“在此处输入图像描述”

使用的代码是下一个。但是,我不知道如何处理绘制的不同颜色。如果我将参数颜色在线上更改,

 ggplot(data)+geom_point(aes(x=variable,y=value,color=clinical.pathway),size=1.5,show.legend = 
 F)+
  geom_line(aes(x=variable,y=value,color=code2),show.legend=F)+
  facet_wrap(~clinical.pathway)

如果我将其作为颜色参数临床。路径我会得到错误的图:

  ggplot(data)+geom_point(aes(x=variable,y=value,color=clinical.pathway),size=1.5,show.legend 
  = 
   F)+
   geom_line(aes(x=variable,y=value,color=clinical.pathway),show.legend=F)+
   facet_wrap(~clinical.pathway)

“在此处输入图像描述”

一些来处理它的示例数据是下一个:

structure(list(code2 = c("1234567", "6010480", "6011819", "6011122", 
                         "6011250", "6013164", "6014847", "6005343", "6009382", "6004114", 
                         "6013375", "6004627", "6006660", "6011440", "6012721", "6009379", 
                         "6002612", "6006652", "6009384", "6004392", "6010087", "6010592", 
                         "6010056", "6010733", "6009028", "1231234", "6011250", "6006607", 
                         "6002612", "1234567", "6013595", "6008624", "6008624", "6014886", 
                         "6012847", "6010859", "6012437", "6013322", "6011382", "6004755", 
                         "6010758", "6012818", "6014147", "6004334", "6014223", "6012656", 
                         "6004669", "6009028", "6007505", "6011445", "6010106", "6011135", 
                         "6014567", "6010055", "6005026", "6005158", "6014426", "6013595", 
                         "6014148", "6010307"), clinical.pathway = c("Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Spine", "Primary Rehabilitation", 
                                                                     "Breding", "Pain Treatment", "Breding", "Breding", 
                                                                     "Breding", "Primary Rehabilitation", "Pain Treatment", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Breding", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Breding", "Breding", "Primary Rehabilitation", "Breding", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Lectures", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Breding", "Breding", "Breding", 
                                                                     "Primary Rehabilitation", "Breding", "Primary Rehabilitation", 
                                                                     "Breding", "Cooking", "Cooking", 
                                                                     "Cooking", "Spine", "Cooking", 
                                                                     "Lectures", "Cooking", "Cooking", 
                                                                     "Cooking", "Cooking", "Pain Treatment", 
                                                                     "Cooking"), variable = c(1, 4, 3, 4, 4, 1, 2, 
                                                                                                             4, 4, 3, 4, 3, 1, 3, 3, 3, 2, 3, 3, 2, 2, 3, 4, 2, 3, 4, 3, 3, 
                                                                                                             1, 2, 1, 1, 3, 4, 4, 1, 1, 3, 3, 1, 2, 3, 3, 4, 2, 1, 1, 2, 1, 
                                                                                                             1, 3, 2, 4, 1, 2, 1, 4, 3, 1, 1), value = c(9, 20, 49, 41, 31, 
                                                                                                                                                         28, 29, 21, NA, 15, 30, 51, 77, 48, 29, 36, 22, 36, 67, 0, 16, 
                                                                                                                                                         12, 98, 30, 45, 17, 24, 14, 19, 45, 15, 15, 16, 60, 63, 10, 67, 
                                                                                                                                                         22, 27, 30, 43, 23, 24, 67, 20, 38, 0, 53, 39, 28, 70, 17, 56, 
                      

                                                               

I have the next dataset.

  code2       clinical.pathway variable value
1  6002536 Primary Rehabilitation        1    19
2  6002536 Primary Rehabilitation        2    23
3  6002536 Primary Rehabilitation        3    22
4  6002536 Primary Rehabilitation        4    76
5  6002598 Primary Rehabilitation        1    16
6  6002598 Primary Rehabilitation        2    19
7  6002598 Primary Rehabilitation        3    51
8  6002598 Primary Rehabilitation        4    83
9  6002602           Hand Surgery        1    68
10 6002602           Hand Surgery        2    29
11 6002602           Hand Surgery        3    29
12 6002602           Hand Surgery        4    71
 .....

In clinical pathway I have more factors, in this case 4 more.

I am dealing with the next plot considering as repeated measures.

   ggplot(data)+
     geom_point(aes(x=variable,y=value,color=clinical.pathway),size=1.5,show.legend = F)+
     facet_wrap(~clinical.pathway)

enter image description here

I would like to join every point with a line of the same colour as the point.
Something like this:

enter image description here

The code used was the next down. But, I dont know how to deal with the different colors plotted. If I change the parameter color in line I get a diferent result

 ggplot(data)+geom_point(aes(x=variable,y=value,color=clinical.pathway),size=1.5,show.legend = 
 F)+
  geom_line(aes(x=variable,y=value,color=code2),show.legend=F)+
  facet_wrap(~clinical.pathway)

If I put as color parameter clinical.pathway I get a wrong plot like this:

  ggplot(data)+geom_point(aes(x=variable,y=value,color=clinical.pathway),size=1.5,show.legend 
  = 
   F)+
   geom_line(aes(x=variable,y=value,color=clinical.pathway),show.legend=F)+
   facet_wrap(~clinical.pathway)

enter image description here

Some sample data to deal with it is the next one:

structure(list(code2 = c("1234567", "6010480", "6011819", "6011122", 
                         "6011250", "6013164", "6014847", "6005343", "6009382", "6004114", 
                         "6013375", "6004627", "6006660", "6011440", "6012721", "6009379", 
                         "6002612", "6006652", "6009384", "6004392", "6010087", "6010592", 
                         "6010056", "6010733", "6009028", "1231234", "6011250", "6006607", 
                         "6002612", "1234567", "6013595", "6008624", "6008624", "6014886", 
                         "6012847", "6010859", "6012437", "6013322", "6011382", "6004755", 
                         "6010758", "6012818", "6014147", "6004334", "6014223", "6012656", 
                         "6004669", "6009028", "6007505", "6011445", "6010106", "6011135", 
                         "6014567", "6010055", "6005026", "6005158", "6014426", "6013595", 
                         "6014148", "6010307"), clinical.pathway = c("Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Spine", "Primary Rehabilitation", 
                                                                     "Breding", "Pain Treatment", "Breding", "Breding", 
                                                                     "Breding", "Primary Rehabilitation", "Pain Treatment", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Breding", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Breding", "Breding", "Primary Rehabilitation", "Breding", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Lectures", 
                                                                     "Primary Rehabilitation", "Primary Rehabilitation", "Primary Rehabilitation", 
                                                                     "Primary Rehabilitation", "Breding", "Breding", "Breding", 
                                                                     "Primary Rehabilitation", "Breding", "Primary Rehabilitation", 
                                                                     "Breding", "Cooking", "Cooking", 
                                                                     "Cooking", "Spine", "Cooking", 
                                                                     "Lectures", "Cooking", "Cooking", 
                                                                     "Cooking", "Cooking", "Pain Treatment", 
                                                                     "Cooking"), variable = c(1, 4, 3, 4, 4, 1, 2, 
                                                                                                             4, 4, 3, 4, 3, 1, 3, 3, 3, 2, 3, 3, 2, 2, 3, 4, 2, 3, 4, 3, 3, 
                                                                                                             1, 2, 1, 1, 3, 4, 4, 1, 1, 3, 3, 1, 2, 3, 3, 4, 2, 1, 1, 2, 1, 
                                                                                                             1, 3, 2, 4, 1, 2, 1, 4, 3, 1, 1), value = c(9, 20, 49, 41, 31, 
                                                                                                                                                         28, 29, 21, NA, 15, 30, 51, 77, 48, 29, 36, 22, 36, 67, 0, 16, 
                                                                                                                                                         12, 98, 30, 45, 17, 24, 14, 19, 45, 15, 15, 16, 60, 63, 10, 67, 
                                                                                                                                                         22, 27, 30, 43, 23, 24, 67, 20, 38, 0, 53, 39, 28, 70, 17, 56, 
                      

                                                               

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

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

发布评论

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

评论(1

童话 2025-02-04 04:18:44

这是你想要的吗?

library(tidyverse)

tribble(
  ~code2, ~clinical.pathway, ~variable, ~value,
  6002536, "Primary Rehabilitation", 1, 19,
  6002536, "Primary Rehabilitation", 2, 23,
  6002536, "Primary Rehabilitation", 3, 22,
  6002536, "Primary Rehabilitation", 4, 76,
  6002598, "Primary Rehabilitation", 1, 16,
  6002598, "Primary Rehabilitation", 2, 19,
  6002598, "Primary Rehabilitation", 3, 51,
  6002598, "Primary Rehabilitation", 4, 83,
  6002602, "Hand Surgery", 1, 68,
  6002602, "Hand Surgery", 2, 29,
  6002602, "Hand Surgery", 3, 29,
  6002602, "Hand Surgery", 4, 71,
  6002603, "Hand Surgery", 1, 78,
  6002603, "Hand Surgery", 2, 39,
  6002603, "Hand Surgery", 3, 39,
  6002603, "Hand Surgery", 4, 85
) |> 
  mutate(code2 = factor(code2)) |> 
  ggplot(aes(variable, value, color = clinical.pathway, group = code2))+
  geom_point(size = 1.5) +
  geom_line() +
  facet_wrap(~clinical.pathway) +
  theme(legend.position = "none")

“”

在2022-05-12上由 reprex软件包(v2.0.1)

Is this what you want?

library(tidyverse)

tribble(
  ~code2, ~clinical.pathway, ~variable, ~value,
  6002536, "Primary Rehabilitation", 1, 19,
  6002536, "Primary Rehabilitation", 2, 23,
  6002536, "Primary Rehabilitation", 3, 22,
  6002536, "Primary Rehabilitation", 4, 76,
  6002598, "Primary Rehabilitation", 1, 16,
  6002598, "Primary Rehabilitation", 2, 19,
  6002598, "Primary Rehabilitation", 3, 51,
  6002598, "Primary Rehabilitation", 4, 83,
  6002602, "Hand Surgery", 1, 68,
  6002602, "Hand Surgery", 2, 29,
  6002602, "Hand Surgery", 3, 29,
  6002602, "Hand Surgery", 4, 71,
  6002603, "Hand Surgery", 1, 78,
  6002603, "Hand Surgery", 2, 39,
  6002603, "Hand Surgery", 3, 39,
  6002603, "Hand Surgery", 4, 85
) |> 
  mutate(code2 = factor(code2)) |> 
  ggplot(aes(variable, value, color = clinical.pathway, group = code2))+
  geom_point(size = 1.5) +
  geom_line() +
  facet_wrap(~clinical.pathway) +
  theme(legend.position = "none")

Created on 2022-05-12 by the reprex package (v2.0.1)

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