Home > Mobile >  how can I make plot the top 5 positive and top 5 negative values
how can I make plot the top 5 positive and top 5 negative values

Time:11-29

I have a data looks like this

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))

I am trying to plot in one figure the top 5 positive with their sd and top 5 negative with their sd

any suggestion?

basically when I try to plot, it even forget the sorting

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

CodePudding user response:

Edit 2

If you want to plot all of the data and 'highlight' the top/bottom 5, one option would be:

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())

Created on 2022-11-29 with bar plot

  • Related