2017-07-24 66 views
5

我有麻煩製作下面ggplot的傳說包裹在ggplotly()只顯示一個審美。它目前顯示每個圖例條目的三個變量(shape,color,linetype),但我只想顯示一個。圖例中的額外變量當在包裝ggplot2時劇情R

只有其中一個aes()值在此圖(linetype)中發生變化,但其他值對應於特定變量,並且應在我的網站上的許多圖上保持統一。從我們的案例中簡單地刪除其他aes()值不是一個可行的解決方案,因爲我希望它們在其他類似的情節中更改。此外,隱藏圖例和修改tooltip以顯示信息的作品,但不是最終的結果。

當運行,下面的代碼:

library(ggplot2) 
library(plotly) 

#aes lists 
solute_colors <- c("NO3" = "#BF1616") 
source_shapes <- c("rain"= 21) 
location_linetypes <- c("1"= 2,"2"= 1,"3"= 3) 

#create dataframe 
data <- data.frame(
    date = c(1966, 1970, 1972, 1979, 1989, 1990, 1998, 2000), 
    concentration = sample(1:8), 
    solute = c("NO3", "NO3", "NO3", "NO3", "NO3", "NO3", "NO3", "NO3"), 
    location = c("3", "1", "2", "3", "2", "1", "1", "2"), 
    source = c("rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain") 
) 

#ggplot 
ggplot(data, aes(x= date, y= concentration, linetype= location, color= solute, shape= source))+ 
    geom_point() + 
    geom_line() + 
    scale_shape_manual(values = source_shapes) + 
    scale_color_manual(values = solute_colors)+ 
    guides(shape = F, color = F)+ #removes shape and source legends in ggplot, but not in ggplotly 
    scale_linetype_manual(values = location_linetypes) 

圖例只示出了linetype,這是所希望的結果(見於here)。然而,在ggplotly

#ggplot p 
p<-ggplot(data, aes(x= date, y= concentration, linetype= location, color= solute, shape= source))+ 
    geom_point() + 
    geom_line() + 
    scale_shape_manual(values = source_shapes) + 
    scale_color_manual(values = solute_colors)+ 
    guides(shape = F, color = F)+ #removes shape and source legends in ggplot, but not in ggplotly 
    scale_linetype_manual(values = location_linetypes) 
#wrap p in ggplotly 
ggplotly(p) 

包裹時,它的圖例示出了在圖例中的同一行內三個aes()值如圖

here

如何在覆蓋ggplotly時手動覆蓋圖例中的覆蓋範圍?我在ggplot中添加了更改ggplotggplotly(例如legend.positionlegend.title)的圖例的主題,儘管我沒有找到任何控制顯示的實際變量的內容。

我在Windows 10上使用R版本3.4.0(RStudio版本1.0.143)。任何幫助將不勝感激!

回答

2

不知道如何強制ggplotly尊重你的傳奇標籤,但你可以手動覆蓋幾乎所有東西。

gp <- ggplotly(p) 
gp[['x']][['data']][[1]][['name']] <- '1' 
gp[['x']][['data']][[2]][['name']] <- '2' 
gp[['x']][['data']][[3]][['name']] <- '3' 
gp 

enter image description here