IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 102 forks source link

Legend position not connected #4292

Open mmumbo opened 6 years ago

mmumbo commented 6 years ago

The theme option to change the legend position or to not have a legend does not work. It seems simply that the code is not added when that option is selected

dannyparsons commented 6 years ago

@maxwellfundi can you look into this?

maxwellfundi commented 6 years ago

Yeah..

maxwellfundi commented 6 years ago

I have been looking at this and its funny how it does not add legend positions when the Select Theme checkbox under themes tab in sdgplots is checked. It worked for me when i unchecked it and went to all options.

I think this is slightly confusing. Do we need to uncheck that select theme checkbox before going to All options. I find it working in both ways but with different code. I understand the base theme is overiden by the theme(), but do we want to still keep it on the code?

Code with the checkbox checked # Code generated by the dialog, Scatter Plot survey <- InstatDataObject$get_data_frame(data_name="survey") last_graph <- ggplot2::ggplot(data=survey, mapping=ggplot2::aes(colour=Variety., y=Yield, x=Village.)) + ggplot2::geom_point() + t**heme_grey() + ggplot2::theme(legend.position="bottom", axis.title.x=ggplot2::element_text(angle=90))** InstatDataObject$add_graph(graph_name="last_graph", graph=last_graph, data_name="survey") InstatDataObject$get_graphs(data_name="survey", graph_name="last_graph")

Code with the checkbox unchecked # Code generated by the dialog, Scatter Plot survey <- InstatDataObject$get_data_frame(data_name="survey") last_graph <- ggplot2::ggplot(data=survey, mapping=ggplot2::aes(colour=Variety., x=Village., y=Yield)) + ggplot2::geom_point() + **ggplot2::theme(legend.position="bottom", axis.title.x=ggplot2::element_text(angle=90))** InstatDataObject$add_graph(graph_name="last_graph", graph=last_graph, data_name="survey") InstatDataObject$get_graphs(data_name="survey", graph_name="last_graph")

I think we need to rethink how this works, and improve the functionality. I might have lost track why this was implemented this way though.

dannyparsons commented 6 years ago

I thought that as long as the theme options come after the selected theme then it is alright. I know that the theme choice can override theme options if it comes at the end, but the code above should be ok for that.

Someone should investigate this in R to see what's going on, maybe we have not understood something about themes. Does it do this for all themes or just some like theme_grey?

rdstern commented 6 years ago

I have been asked here why we can't have a legend with the graphs. I therefore tried adding +theme(legend.position="right") at the end of the line. Nothing happens! When I look at the instructions in R it all looks very simple. I am using the graphics window in R, but hope that isn't an issue. Shouldn't be.

maxwellfundi commented 6 years ago

@rdstern that seems fine.I am surprised Its not working for you. Right is usually a default place where the legend appears. Foe example when i create this scatterplot of yield by village and add variety as the factor it produces the followig graph with the legend on the right using the below code

image

# Code generated by the dialog, Scatter Plot survey <- InstatDataObject$get_data_frame(data_name="survey") scatterplot1 <- ggplot2::ggplot(data=survey, mapping=ggplot2::aes(colour=Variety., y=Yield, x=Village.)) + ggplot2::geom_point() + theme_grey() InstatDataObject$add_graph(graph_name="scatterplot1", graph=scatterplot1, data_name="survey") InstatDataObject$get_graphs(data_name="survey", graph_name="scatterplot1")

By adding the legend part you added at the end it should still work.

rdstern commented 6 years ago

OK, I have made some progress. In your example you have a factor. That works well for the legend. I tried with line plots in 2 ways: 1) Made single into multiple and added a set of lines there. Got a legend. (That's because it stacks and makes a factor.) 2) Kept single and added layers with more lines. No legend to say anything about the lines. 3) Added a factor to the single set. Got a legend for the factor added.

So the problem seems to be with a set of layers when there is no factor. Looking here, for example, it seems then we perhaps have to use scales - this exceeds my level of R! Aha, this seems to be a possible answer - still scales - look here. The question was: how-to-add-a-legend-for-two-geom-layers-in-one-ggplot2, which seems to be what we want.