Additional variables in the legend when wrapping ggplot2 in plotly R

I'm having trouble creating the legend of the next ggplot wrapped in ggplotly() , showing only one aesthetic. It currently displays three variables ( shape , color , linetype ) for each legend entry, but I only want to show it.

Only one of the aes() values ​​changes on this graph ( linetype ), but the other values ​​correspond to specific variables and should be the same in many parts of my site. Just removing the other aes() values ​​from the graph is not a viable solution in my case, since I want them to change on other graphs like this. Also, hiding the legend and modifying the tooltip to show that the information works, but is not the desired end result.

When running the following code:

 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) 

in the legend only linetype , which is the desired result (see here ). However, wrapping it in 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) 

a legend is displayed with three aes() values ​​within one legend line, as shown

here .

How to override this change when wrapping in ggplotly or manually encode it in a legend? I added themes to ggplot that change the legend in both ggplot and ggplotly just fine (like legend.position and legend.title ), although I did not find anything to control the actual variables displayed.

I am using R version 3.4.0 (RStudio version 1.0.143) on Windows 10. Any help would be greatly appreciated!

+5
source share
1 answer

I don’t know how to force ggplotly keep your legend labels, but you can manually overwrite almost everything.

 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

+2
source

Source: https://habr.com/ru/post/1270165/


All Articles