1

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)

enter image description here

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

Dominique
  • 107
  • 2
  • 13
  • 1
    To get an legend in ggplot you need to be mapping a variable to `color`, `fill`, or `linetype` within `aes`. Plotly is probably overriding your spacing, and so you should use `layout` to adjust it. Also, you should provide enough data in your question to make it [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – alistaire Sep 22 '16 at 13:53
  • You can check all settings by running `p$data` and `p$layout` in your console, where `p` is your plotly object. The data is human readable. You can also change settings this way, for example: `p$layout$title <- "My graph title"`. Not all possible settings are listed only those with a value. For more check the plotly page – Siemkowski Sep 22 '16 at 14:06
  • @alistaire, thanks for your advice. I added a subset of my data so you can try reproduce it – Dominique Sep 22 '16 at 16:08

1 Answers1

2

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

Community
  • 1
  • 1
MelissaG
  • 115
  • 1
  • 11