ggplotly() игнорирует легенду и создает другой график с легендой ggplot

Я пытаюсь использовать ggplotly для запуска графика ggplot, но метка легенды не показывает то же самое. Почему это так? Пожалуйста помоги. Спасибо. А также любая идея игнорировать предупреждение о переходе на числовые данные, чтобы не отображалось слишком много предупреждений при запуске через блестящую. Большое спасибо

GGPLOT

GGPLOTLY

Код ниже

structure(list(...1 = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", 
"31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", 
"42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", 
"53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", 
"64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", 
"75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", 
"86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", 
"97", "98", "99", "100", "101", "102", "103", "104", "105", "106", 
"107", "108", "109", "110", "111", "112", "113", "114", "115", 
"116", "117", "118", "119", "120", "121", "122", "123", "124", 
"125", "126", "127", "128", "129", "130", "131", "132", "133", 
"134", "135", "136", "137", "138", "139", "140", "141", "142", 
"143", "144", "145", "146", "147", "148", "149"), indexlist = c(1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 
139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149), datainput = c("112069", 
"7377.02", "Unanswered", "675900", "Unanswered", "17323000", 
"1935328.98", "411079", "Unanswered", "38530.29", "96.5", "89268", 
"6380000", "32185.99", "102103", "Unanswered", "Question no match", 
"Unanswered", "Unanswered", "1441914.2080000001", "681325", "89340.307000000001", 
"234", "9278", "9809", "259550", "675900", "Unanswered", "168322", 
"Unanswered", "435708.78", "962.15899999999999", "681325", "81000", 
"38759", "Unanswered", "Question no match", "Unanswered", "195747", 
"Unanswered", "7070890", "10739506", "65430.91", "Unanswered", 
"61900", "Unanswered", "Unanswered", "5130068", "11556", "Unanswered", 
"Unanswered", "102364", "Unanswered", "103451.19", "9756559.5299999993", 
"16520", "644039", "16.187999999999999", "Unanswered", "Unanswered", 
"13154.44", "Question no match", "Question no match", "125131", 
"Unanswered", "Unanswered", "Unanswered", "608470.29", "Question no match", 
"Unanswered", "Unanswered", "Unanswered", "10496.82", "195747", 
"21399", "Unanswered", "214050", "1439.18", "681104", "10587765", 
"11816", "69528", "Unanswered", "26519409", "Question no match", 
"1013315", "17323000", "114016", "117723", "Unanswered", "Question no match", 
"555872.6", "8442.34", "1995000", "Unanswered", "7208", "152495", 
"372366", "132191.5", "21399", "Unanswered", "195747", "3207.89", 
"Unanswered", "77629", "195747", "Question no match", "Unanswered", 
"400", "Unanswered", "555872.6", "3291303", "110296.5", "Unanswered", 
"55715.991999999998", "186011", "Unanswered", "Question no match", 
"Unanswered", "385000", "Unanswered", "142829.75599999999", "125131", 
"Question no match", "20981", "Unanswered", "186011", "9701.8629999999994", 
"Unanswered", "102103", "5138", "4395555.97", "118398.916", "1638.58", 
"2749023", "Unanswered", "9394598", "20960", "17323000", "1232.19", 
"240468", "6963.1", "Unanswered", "348.99400000000003", "2513000", 
"4449880.6100000003", "Unanswered", "Unanswered", "27522854"), 
    verification = c("Yes", "no information", "no answer", "Yes", 
    "no answer", "Yes", "Yes", "Yes", "no information", "no information", 
    "no information", "no information", "Yes", "Yes", "Yes", 
    "no answer", "No", "no information", "no answer", "Yes", 
    "Yes", "no information", "no information", "Yes", "Yes", 
    "Yes", "Yes", "no answer", "No", "no answer", "no information", 
    "no information", "Yes", "no information", "Yes", "no answer", 
    "No", "no information", "Yes", "no answer", "Yes", "Yes", 
    "Yes", "no answer", "Yes", "no answer", "no answer", "No", 
    "No", "no answer", "no information", "Yes", "no answer", 
    "Yes", "Yes", "Yes", "Yes", "No", "no answer", "no answer", 
    "Yes", "no information", "No", "No", "no information", "no answer", 
    "no answer", "No", "no information", "no answer", "no answer", 
    "no information", "No", "Yes", "No", "no answer", "Yes", 
    "Yes", "Yes", "Yes", "no information", "Yes", "no answer", 
    "Yes", "no information", "Yes", "Yes", "Yes", "Yes", "no answer", 
    "no information", "No", "no information", "Yes", "no answer", 
    "Yes", "Yes", "Yes", "Yes", "No", "no answer", "Yes", "Yes", 
    "no answer", "No", "Yes", "no information", "no answer", 
    "no information", "no answer", "No", "Yes", "No", "no information", 
    "No", "no answer", "no answer", "no information", "no answer", 
    "Yes", "no answer", "Yes", "No", "no information", "Yes", 
    "no answer", "no answer", "No", "no answer", "Yes", "no information", 
    "Yes", "No", "Yes", "Yes", "no answer", "Yes", "Yes", "Yes", 
    "Yes", "No", "No", "no answer", "no information", "Yes", 
    "Yes", "no answer", "no answer", "Yes")), row.names = c(NA, 
-149L), class = c("tbl_df", "tbl", "data.frame"))->data_a

    p <- data_a%>%
  select(indexlist, datainput, verification) %>%
  mutate_at(c("datainput"), as.numeric)%>%
  drop_na(c("datainput"))%>%
    ggplot(aes(x=1:length(`datainput`), y=`datainput`, label= `indexlist`, color = `verification` == "Yes"))+
  scale_colour_manual(name = 'Verification',breaks = c("TRUE", "FALSE"), values = c("green", "red"), labels = c("Verified", "Non-Verified"))+
    geom_point(size=1.5, alpha = 0.4)+ 
  geom_text(aes(label= ifelse(`datainput` > quantile(`datainput`, 0.975,na.rm = T), `indexlist`,"")),  vjust = "inward", hjust = "inward", size = 2, color = "grey50")+
  theme_minimal()+
  labs(title = "datainput Details",
       x = "",
       y = "")+
  theme(
  axis.text.x = element_text(size = 5.5),
  axis.text.y = element_text(size = 5.5),
  plot.title = element_text(color = "grey40", size = 9, face = "bold"))

ggplotly(p)

Я пробовал scale_manual_fill и color, но это не работает


person mjberlin15    schedule 17.02.2021    source источник


Ответы (2)


Попытайтесь отделить очистку/подготовку данных от построения графика, см. очищенные данные и график, теперь ggplot и plotly выглядят одинаково:

library(tidyverse)
library(plotly)

# prepare the data
plotData <- data_a %>%
  select(indexlist, datainput, verification) %>%
  # remove non-numeric rows before converting
  filter(!grepl("^[^0-9.]+$", datainput)) %>% 
  # prepare data for plotting
  mutate(datainput = as.numeric(datainput),
         x = seq(n()),
         Verification = factor(ifelse(verification == "Yes", "Verified", "Non-Verified"),
                               levels = c("Verified", "Non-Verified")),
         label = ifelse(datainput > quantile(datainput, 0.975, na.rm = TRUE),
                        indexlist, ""))

# then plot with clean data
p <- ggplot(plotData, aes(x = x, y = datainput, 
                          color = Verification, label = label)) +
  scale_colour_manual(values = c("green", "red"))+
  geom_point(size = 1.5, alpha = 0.4) + 
  geom_text(vjust = "inward", hjust = "inward", size = 2, color = "grey50") +
  theme_minimal() +
  labs(title = "datainput Details", x = "", y = "") +
  theme(axis.text.x = element_text(size = 5.5),
        axis.text.y = element_text(size = 5.5),
        plot.title = element_text(color = "grey40", size = 9, face = "bold"))

# now plotly
ggplotly(p)

ggplot

введите здесь описание изображения

сюжетно

введите здесь описание изображения

person zx8754    schedule 17.02.2021

Я попытался очистить ваш процесс обработки данных, подготовив данные перед построением графика.

library(dplyr)
library(plotly)
library(ggplot2)

p1 <- data_a %>%
  filter(grepl('\\d+', datainput)) %>%
  mutate(datainput = as.numeric(datainput), 
         row = as.numeric(`...1`), 
         verification = ifelse(verification == 'Yes', 'verified', 'Non-Verified')) %>%
  ggplot(aes(row, datainput, color = verification)) +
  scale_colour_manual(name = 'Verification',
                      values = c("green", "red")) + 
  geom_point(size=1.5, alpha = 0.4)+ 
  geom_text(aes(label= ifelse(datainput > quantile(datainput, 0.975,na.rm = TRUE), indexlist,"")), 
            vjust = -2, hjust = "inward", size = 2, color = "grey50") +
  theme_minimal()+
  labs(title = "datainput Details",
       x = "",
       y = "")+
  theme(
    axis.text.x = element_text(size = 5.5),
    axis.text.y = element_text(size = 5.5),
    plot.title = element_text(color = "grey40", size = 9, face = "bold"))

plotly::ggplotly(p1)

введите здесь описание изображения

person Ronak Shah    schedule 17.02.2021