简体   繁体   中英

Issues with ggplot in ggplotly package in R: missing legend and no spaces between axis and labels

When I run the following code, it produces this graph:

plot <- ggplot(dat, aes(x = HeightUnderDebris, y = GrassHeight)) + 
    geom_point() +
    stat_smooth(method = 'lm', se = FALSE,color = 'darkgreen') +
    stat_smooth(aes(x = HeightUnderDebris, y = 5, linetype = "Linear Fit"), 
                method = "lm", formula = y ~ x, se = F, size = 1, color = 'lightgreen') +
    labs(x = "Height under CWD (cm)", y = "Grass Height (cm)")+
    scale_fill_manual(name = 'My Lines', values = c("darkgreen", "lightgreen")) +
    theme(axis.title.x = element_text(color = "black", vjust = -1),
          axis.title.y = element_text(vjust = 0.5))
ggplotly(plot)

在此处输入图片说明

For some reason, I cannot increase the spaces between the axis labels and graph, even though I have tried to many different ways using vjust . And I can see some semblance of a legend in the right hand corner. But I cant see the entire thing nor can I zoom out. Is there any issue with my above code?

This is a subset of my data:

GrassHeight HeightUnderCWD 0 0 0 0 0 0 8 16 0 0 0 0 0 0 2 2 6 6 0 0 0 0 1 1 0 0 0 0 0 0 8 15 0 0 7 7 15 15

If you look at the plot object by itself you will see that is missing the legend you defined with scale_fill_manual named 'My Lines' so there is something wrong with your ggplot code before you convert it. Instead it is printing 'Linear Fit' from your second stat_smooth layer (See ?linetype for valid values of linetype.)

To correct try putting your color in your aes mapping (similiar to what Alistaire highlighted).

Reference: ggplot2: missing legend and how to add?

Then you'll also want to use scale_***_manual 'color' instead of 'fill' to create custom legend. This matches the aes you mapped earlier with stat_smooth.

Reference: R: Custom Legend for Multiple Layer ggplot

Revised code:

plot <-   ggplot(dat, aes(x = HeightUnderCWD, y = GrassHeight)) + 
    geom_point() + 
    stat_smooth(aes(color = 'darkgreen'),method = 'lm', se = FALSE,) +  
    stat_smooth(aes(x = HeightUnderCWD, y = 5,color='lightgreen'),
         method = "lm", formula = y ~ x, se = F, size = 1) + 
    scale_color_manual(name = 'My Lines', 
        values =c("darkgreen"="darkgreen", "lightgreen"="lightgreen"),
        labels=c("GrassHeight 5cm","Linear Fit")) +
    labs(x = "Height under CWD (cm)", y = "Grass Height (cm)")+
    theme(axis.title.x = element_text(color = "black", vjust = -1),
        axis.title.y = element_text(vjust = 0.5))

#check plot
plot

ggplotly(plot)

If you still don't like the look after you convert it to plotly, you can adjust margins/padding on your plotly object using the 'layout' function. You don't need to save the object and modify object details directly. The examples on the plot.ly site show how to add without saving first.

Example command using their examples:

ggplotly(plot) %>% layout(autosize=F,margin=list(l=50,r=50,b=50,t=50,pad=5))

References:
https://plot.ly/r/setting-graph-size/
https://plot.ly/r/reference/#layout-margins

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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