R语言 如何绘制前5个正值和前5个负值

wpx232ag  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(211)

我有一个数据看起来像这样

df <- structure(list(Data = c("D1", "D2", "D3", "D4", "D5", "D6", "D7", 
"D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15", "D16", 
"D17", "D18", "D19", "D20", "D21", "D22", "D23", "D24", "D25", 
"D26", "D27", "D28", "D29", "D30", "D31", "D32", "D33", "D34", 
"D35", "D36", "D37", "D38", "D39", "D40", "D41", "D42", "D43", 
"D44", "D45", "D46", "D47", "D48", "D49", "D50", "D51", "D52", 
"D53", "D54", "D55", "D56", "D57", "D58", "D59", "D60", "D61", 
"D62", "D63", "D64", "D65", "D66", "D67", "D68", "D69", "D70", 
"D71", "D72", "D73", "D74", "D75", "D76", "D77", "D78", "D79", 
"D80", "D81", "D82", "D83", "D84", "D85", "D86", "D87", "D88", 
"D89", "D90", "D91", "D92", "D93", "D94", "D95", "D96", "D97", 
"D98"), Response = c(18.11, 17.6, 16.14, 16.02, 14.73, 14.07, 
13.41, 13.08, 12.73, 12.51, 11.53, 9.85, 9.78, 9.74, 8.98, 1.54, 
1.54, 1.51, 1.51, 1.49, 1.49, 1.49, 1.44, 1.44, 1.39, 1.37, 1.36, 
1.35, 1.34, 1.34, 1.33, 1.33, 1.31, 1.3, 1.29, 1.26, 1.23, 1.21, 
1.19, 1.18, 1.14, 1.12, 1.07, 1.05, 1.03, 1.03, 1.03, 1.02, 1.01, 
1, 0.99, 0.98, 0.95, 0.94, 0.93, 0.92, 0.91, 0.87, 0.83, 0.83, 
0.81, 0.8, 0.8, 0.79, 0.78, 0.76, 0.76, 0.75, 0.75, 0.73, 0.73, 
0.71, 0.69, 0.68, 0.67, 0.67, 0.64, 0.63, 0.63, -11.6, -11.6, 
-11.6, -11.7, -11.7, -11.7, -11.8, -11.9, -11.9, -12, -12.2, 
-12.3, -12.4, -12.4, -12.5, -12.5, -12.6, -12.6, -12.7), SD = c(8.98, 
8.19, 2.57, 8.36, 2.14, 5.51, 3.01, 9.8, 6.88, 3.21, 11.35, 5.6, 
13.29, 4.9, 9.45, 11.06, 5.41, 28.78, 13.46, 4.23, 12.24, 1.41, 
11.03, 11.29, 2.17, 7.11, 3.08, 1.8, 12.09, 4.79, 1.01, 3.69, 
3.62, 3.06, 3.12, 13.19, 4.28, 12.05, 3.96, 13.4, 3.02, 7.97, 
12, 3.91, 1.85, 3.96, 1.79, 2.67, 0.37, 7.45, 9.57, 1.95, 1.82, 
3.02, 6.97, 6.73, 1.22, 5.45, 4.9, 2.48, 6.88, 3.81, 1.86, 1.83, 
7.56, 11.25, 2.72, 3.68, 2.41, 4.44, 2.59, 6.24, 2.56, 3.96, 
10.56, 1.65, 7.22, 4.12, 4.01, 3.4, 2.8, 0.92, 9.58, 1.17, 1.36, 
0.91, 5.8, 4.26, 3.48, 3.7, 11.99, 5.77, 11.06, 3.09, 0.12, 3.18, 
5.49, 5.81)), class = "data.frame", row.names = c(NA, -98L))

我试图在一个图中绘制前5个积极与他们的sd和前5个消极与他们的sd
有什么建议吗?
基本上当我试图绘图时,它甚至忘记了排序

ggplot(data=df, aes(x=Data, y=Response,group=1)) +
    geom_line()+
    geom_point()
bq9c1y66

bq9c1y661#

编辑2

如果要绘制所有数据并“突出显示”前/后5项,一个选项是:

library(tidyverse)
library(ggrepel)

df <- structure(list(Data = c("D1", "D2", "D3", "D4", "D5", "D6", "D7", 
                              "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15", "D16", 
                              "D17", "D18", "D19", "D20", "D21", "D22", "D23", "D24", "D25", 
                              "D26", "D27", "D28", "D29", "D30", "D31", "D32", "D33", "D34", 
                              "D35", "D36", "D37", "D38", "D39", "D40", "D41", "D42", "D43", 
                              "D44", "D45", "D46", "D47", "D48", "D49", "D50", "D51", "D52", 
                              "D53", "D54", "D55", "D56", "D57", "D58", "D59", "D60", "D61", 
                              "D62", "D63", "D64", "D65", "D66", "D67", "D68", "D69", "D70", 
                              "D71", "D72", "D73", "D74", "D75", "D76", "D77", "D78", "D79", 
                              "D80", "D81", "D82", "D83", "D84", "D85", "D86", "D87", "D88", 
                              "D89", "D90", "D91", "D92", "D93", "D94", "D95", "D96", "D97", 
                              "D98"), Response = c(18.11, 17.6, 16.14, 16.02, 14.73, 14.07, 
                                                   13.41, 13.08, 12.73, 12.51, 11.53, 9.85, 9.78, 9.74, 8.98, 1.54, 
                                                   1.54, 1.51, 1.51, 1.49, 1.49, 1.49, 1.44, 1.44, 1.39, 1.37, 1.36, 
                                                   1.35, 1.34, 1.34, 1.33, 1.33, 1.31, 1.3, 1.29, 1.26, 1.23, 1.21, 
                                                   1.19, 1.18, 1.14, 1.12, 1.07, 1.05, 1.03, 1.03, 1.03, 1.02, 1.01, 
                                                   1, 0.99, 0.98, 0.95, 0.94, 0.93, 0.92, 0.91, 0.87, 0.83, 0.83, 
                                                   0.81, 0.8, 0.8, 0.79, 0.78, 0.76, 0.76, 0.75, 0.75, 0.73, 0.73, 
                                                   0.71, 0.69, 0.68, 0.67, 0.67, 0.64, 0.63, 0.63, -11.6, -11.6, 
                                                   -11.6, -11.7, -11.7, -11.7, -11.8, -11.9, -11.9, -12, -12.2, 
                                                   -12.3, -12.4, -12.4, -12.5, -12.5, -12.6, -12.6, -12.7), SD = c(8.98, 
                                                                                                                   8.19, 2.57, 8.36, 2.14, 5.51, 3.01, 9.8, 6.88, 3.21, 11.35, 5.6, 
                                                                                                                   13.29, 4.9, 9.45, 11.06, 5.41, 28.78, 13.46, 4.23, 12.24, 1.41, 
                                                                                                                   11.03, 11.29, 2.17, 7.11, 3.08, 1.8, 12.09, 4.79, 1.01, 3.69, 
                                                                                                                   3.62, 3.06, 3.12, 13.19, 4.28, 12.05, 3.96, 13.4, 3.02, 7.97, 
                                                                                                                   12, 3.91, 1.85, 3.96, 1.79, 2.67, 0.37, 7.45, 9.57, 1.95, 1.82, 
                                                                                                                   3.02, 6.97, 6.73, 1.22, 5.45, 4.9, 2.48, 6.88, 3.81, 1.86, 1.83, 
                                                                                                                   7.56, 11.25, 2.72, 3.68, 2.41, 4.44, 2.59, 6.24, 2.56, 3.96, 
                                                                                                                   10.56, 1.65, 7.22, 4.12, 4.01, 3.4, 2.8, 0.92, 9.58, 1.17, 1.36, 
                                                                                                                   0.91, 5.8, 4.26, 3.48, 3.7, 11.99, 5.77, 11.06, 3.09, 0.12, 3.18, 
                                                                                                                   5.49, 5.81)), class = "data.frame", row.names = c(NA, -98L))
df$Data <- factor(df$Data, levels = paste0("D", 1:98), ordered = TRUE)

ggplot() +
  geom_line(data = df, aes(x = Data, y = Response, group = 1),
            alpha = 0.25) +
  geom_point(data = df, aes(x = Data, y = Response, group = 1),
             alpha = 0.25) +
  geom_line(data = df %>% slice_max(order_by = Response, n = 5),
            aes(x = Data, y = Response, group = 1)) +
  geom_point(data = df %>% slice_max(order_by = Response, n = 5),
             aes(x = Data, y = Response, group = 1)) +
  geom_linerange(data = df %>% slice_max(order_by = Response, n = 5),
                 aes(x = Data, y = Response,
                     ymin = Response - SD, ymax = Response + SD)) +
  geom_text_repel(data = df %>% slice_max(order_by = Response, n = 5),
                  aes(x = Data, label = Data, y = Response),
                  min.segment.length = 0, xlim = 10,
                  force = 5, color = "firebrick3") +
  geom_line(data = df %>% slice_min(order_by = Response, n = 5),
            aes(x = Data, y = Response, group = 1)) +
  geom_point(data = df %>% slice_min(order_by = Response, n = 5),
             aes(x = Data, y = Response, group = 1)) +
  geom_linerange(data = df %>% slice_min(order_by = Response, n = 5),
                 aes(x = Data, y = Response,
                     ymin = Response - SD, ymax = Response + SD)) +
  geom_text_repel(data = df %>% slice_min(order_by = Response, n = 5),
                  aes(x = Data, label = Data, y = Response),
                  min.segment.length = 0, xlim = 80,
                  force = 9, color = "firebrick3") +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

创建于2022年11月29日,使用reprex v2.0.2

e5njpo68

e5njpo682#

也许可以试试条形图。由于数据已经排序,所以前5个/后5个值是通过headtail从数据中提取的。

library(ggplot2)

ggplot(rbind(head(df, 5), tail(df, 5))) + 
  geom_col(aes(Data, Response)) + 
  geom_errorbar(aes(Data, ymin = Response - SD, ymax = Response + SD), width=.2)

为中间数据点添加了条形:

dat <- rbind(head(df, 5), 
  list("D6_MEAN", mean(df[-c(1:5, (nrow(df)-4):(nrow(df))), 2]),
    mean(df[-c(1:5, (nrow(df)-4):(nrow(df))), 3])), tail(df, 5))

dat$grp <- c(rep("top", 5), 
  paste("mean of", nrow(df)-10, "rows"), rep("bottom", 5))

dat$grp <- factor(dat$grp, levels=unique(dat$grp))

ggplot(dat) + 
  geom_col(aes(Data, Response, fill=grp)) + 
  geom_errorbar(aes(Data, ymin = Response - SD, ymax = Response + SD), width=.2)

相关问题