繁体   English   中英

ggplotly() 忽略图例并使用 ggplot 图例生成不同的 plot

[英]ggplotly() ignores legend and produce different plot with ggplot legend

我尝试使用 ggplotly 运行 ggplot 图,但图例 label 没有显示相同的内容。 为什么会这样? 请帮忙。 谢谢 还有任何想法忽略更改为数字数据的警告,因此在通过 shiny 运行时不会显示太多警告。 非常感谢

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 但它不起作用

我已经尝试通过在绘图前准备数据来清理您的数据操作过程。

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)

在此处输入图像描述

尝试将数据清理/准备与绘图分开,请参阅清理数据和 plot,现在ggplotplotly看起来相同:

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

在此处输入图像描述

plotly

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM