首页 > 解决方案 > ggplot中的附加点

问题描述

我想通过 连接这些不同的点geom_line。这没用。有人可以帮我找出问题吗?

df_st <- cbind(ID = c("ID_201","ID_202","ID_203","ID_204","ID_205","ID_206","ID_207","ID_208",
                      "ID_209","ID_210","ID_211","ID_212"),
               PARAM_1 = c(48.4,17.6,19.2,23.6,23.7,17.8,16.5,18.2,17.6,19.7,14.3,15.7),
               PARAM_2 = c(14.615,8.06,7.83,10.81,10.635,9.44,7.54,8.86,6.855,8.68,7.36,6.695),
               PARAM_3 = c(19.8,10.3,10.2,13.6,13.8,11.9,9.4,11.2,8.9,11.3,9.0,9.0)) %>% data.frame
 df_st <- df_st %>%
  mutate_at(vars(-ID), as.character)

df_st <- df_st %>%
  mutate_at(vars(-ID), as.numeric)

df_st_g <- df_st %>%
  dplyr::select(ID, PARAM_1,PARAM_2, PARAM_3) %>%
  gather(key = "variable", value = "value",PARAM_1:PARAM_3)

ggplot(df_st_g, aes(x = ID, y = value)) + 
  geom_point(aes(color = variable)) +
  theme_classic() ```

标签: rggplot2

解决方案


你的意思是这样的:

library(tidyverse)
library(tidyr)

#Data
df_st <- structure(list(ID = c("ID_201", "ID_202", "ID_203", "ID_204", 
"ID_205", "ID_206", "ID_207", "ID_208", "ID_209", "ID_210", "ID_211", 
"ID_212"), PARAM_1 = c(48.4, 17.6, 19.2, 23.6, 23.7, 17.8, 16.5, 
18.2, 17.6, 19.7, 14.3, 15.7), PARAM_2 = c(14.615, 8.06, 7.83, 
10.81, 10.635, 9.44, 7.54, 8.86, 6.855, 8.68, 7.36, 6.695), PARAM_3 = c(19.8, 
10.3, 10.2, 13.6, 13.8, 11.9, 9.4, 11.2, 8.9, 11.3, 9, 9)), row.names = c(NA, 
-12L), class = "data.frame")
df2 <- pivot_longer(df_st,cols = names(df_st)[-1])
#Plot
ggplot(df2, aes(x = ID, y = value,group=name,color=name)) + 
  geom_point() +
  geom_line() +
  theme_classic() 

在此处输入图像描述


推荐阅读