簡體   English   中英

將 geom_line 和 geom_point 與 ggplotly 一起使用時出現奇怪的圖例和工具提示文本

[英]Odd legend and tooltip text when using both geom_line and geom_point with ggplotly

我的圖例中出現了奇怪的元素,並且通過懸停顯示的工具提示文本具有隨機的顏色和形狀線條。

樣本數據:

> dput(sample_n(Plot, 20))
structure(list(Strain = c(0.0099576, 0.00550752, 0.0255111, 0.00341572, 
0.000323357, 0.0036487, 0.0265925, 0.00438567, 0.00437111, 0.0214385, 
0.00957133, 0.00144209, 0.0237199, 0.00290038, 0.00786047, 0.0114374, 
0.000582628, 0.00961509, 0.0110853, 0.00333173), Stress = c(74.4928907635218, 
65.7454752463861, 86.6080612547546, 63.8155559103337, 15.8799350673083, 
63.7937705218785, 76.1766851002328, 64.1491550648465, 64.0561929873925, 
84.7109031517226, 64.7832595395012, 46.2442265654563, 85.8590430135155, 
63.3618876683043, 71.5077936431304, 76.5758067207865, 26.3220708755176, 
74.1047449875333, 76.1658755563428, 63.6570713668388), Instrumentation = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("DIC", "Gauge"), class = "factor"), Bar = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("Spliced", "Unspliced"), class = "factor")), row.names = c(NA, 
-20L), class = "data.frame")

我嘗試通過子集來 plot 數據,以便我可以組合geom_pointgeom_line 然而,這似乎使傳說變得復雜。 scale_color_manual 中指定的scale_color_manual僅適用於geom_line ,因此我必須為 geom_point 中的點定義顏色,現在我在geom_point的文本中獲得了額外的輸入。

plot <- ggplot(data = subset(Plot, Instrumentation == "DIC"), aes(x = Strain, y = Stress, 
                       colour = Instrumentation, linetype = Bar)) +
 geom_line(size = 0.8) + theme_classic() + 
 geom_point(data = subset(Plot, Instrumentation == "Gauge"), 
            aes(colour = Instrumentation, shape = Bar), colour = "blue", size = 2) +
 labs(x = "Strain (in/in)", y = "Stress (ksi)") + scale_color_manual(values = c("DIC"="red", "LVDT"="blue")) +
 theme_classic() + theme(axis.text =  element_text(color = "black", size = 16), 
       axis.line = element_line(color = "black", size = 0.2), axis.ticks.y = element_line(color = "black", size = 0.2),
       axis.title.y = element_text(color = "black", size = 20, margin = margin(0,40,0,0)), 
       axis.title.x = element_text(color = "black", size = 20, margin = margin(35,0,0,0)),
       legend.title = element_blank(), legend.text = element_text(color = "black", size = 16))

ggplotly(
  p = ggplot2::last_plot(),
  width = NULL,
  height = NULL,
  tooltip = c("Strain","Stress","Instrumentation","Bar"),
  dynamicTicks = FALSE,
  layerData = 1,
  originalData = TRUE,) %>%
  layout(yaxis = list(title = list(text = "Stress (ksi)", standoff = 30L)),
             xaxis = list(title = list(text = "Strain (in/in)",standoff = 30L)),
             legend = list(orientation = "v", x = 0.7, y = 0.13)) 

屏幕截圖顯示了 plot 與當前代碼的外觀。 有沒有更好的方法來分配 colors 而不會弄亂傳說?

在此處輸入圖像描述

我已將您的數據用作df1並在df2中創建了 20 多條記錄。 然后在開頭將工具提示中應顯示的內容定義為text 以下代碼給出了 ggplot object。

df2 <- df1 %>% mutate(Strain=Strain*10, Stress = 1.2*Stress, Instrumentation = "Gauge")

df <- rbind(df1,df2)
mycolors <- c("red","blue", "green", "orange")
text <- paste("Strain:", df$Stress, "\nStress:", df$Strain, "\nInstrumentation:", df$Instrumentation, "\nBar:", df$Bar)

plot <- ggplot(data = df, aes(x = Strain, y = Stress)) +
  geom_line(size = 0.8, aes(color = Instrumentation, linetype=Instrumentation )) + theme_classic() + 
  geom_point(size=2, aes(color=Bar, shape = Bar, text=text))+ # data = subset(df, Instrumentation == "Gauge"), 
             # aes(colour = Instrumentation, shape = Bar), colour = "blue", size = 2) +
  labs(x = "Strain (in/in)", y = "Stress (ksi)") + 
  scale_color_manual(values = c(mycolors[1:2],mycolors[1:2]) ) + # c("DIC"="red", "LVDT"="blue")) +  # 
  scale_linetype_manual(values = c(1,3)) +
  theme_classic() + theme(axis.text =  element_text(color = "black", size = 16), 
                          axis.line = element_line(color = "black", size = 0.2), axis.ticks.y = element_line(color = "black", size = 0.2),
                          axis.title.y = element_text(color = "black", size = 20, margin = margin(0,40,0,0)), 
                          axis.title.x = element_text(color = "black", size = 20, margin = margin(35,0,0,0)),
                          legend.title = element_blank(), legend.text = element_text(color = "black", size = 12))  +
  guides(linetype = guide_legend(override.aes=list(color=mycolors[1:2], pt.cex=1.5, reverse=T)),  
         shape = guide_legend(override.aes=list(color=mycolors[1:2], lty=0, pt.cex=1.5, reverse=T)), 
         color='none')

plot

輸出

如果這是所需的 output,您可以將 plotly object 創建為

pp <- ggplotly(plot, tooltip=c("text")) 
pp

輸出

接下來,要修復圖例標簽,可以如下所示對其進行黑客攻擊。 雖然這不是一個優雅的解決方案,但它確實有效。

ins <- unique(df$Instrumentation)
br <- unique(df$Bar)

pp$x$data[[1]]$name <- ins[1]
pp$x$data[[2]]$name <- ins[2]
pp$x$data[[3]]$name <- br[1]
pp$x$data[[4]]$name <- br[2]
pp

輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM