Each element of the list returned by conditional_effects
is a ggplot, so you can use any ggplot function on each plot. For example:
# Create list of plots
p = plot(conditional_effects(mod1, effects="age",categorical = T))
# Format the first plot in the list
p[[1]] = p[[1]] +
coord_cartesian(ylim = c(0,.82)) +
labs(x="Age",
title = "Age",
fill = "Age",
colour = "Age")+
theme(axis.title.x=element_blank(),
axis.title.y = element_blank(),
legend.position = "none",
axis.text.x = element_text(color="black", size=4))
If you want to do the same thing to more than one plot, you can use the map
function (from the tidyr
package).
Format plots 1 and 3 (elements 1 and 3 of p
):
p[c(1,3)] = map(p[c(1,3)],
~ .x + coord_cartesian(ylim = c(0,.82)) +
theme(axis.title.x=element_blank(),
axis.title.y = element_blank(),
legend.position = "none",
axis.text.x = element_text(color="black", size=4))
Format all plots:
p = map(p,
~ .x + coord_cartesian(ylim = c(0,.82)) +
theme(axis.title.x=element_blank(),
axis.title.y = element_blank(),
legend.position = "none",
axis.text.x = element_text(color="black", size=4))
And you can, of course, apply common formatting to several or all plots, and custom formatting to any one of them. Note also in the examples above, that (1) all theme changes can be done in a single call to the theme
function, and (2) we only need to run plot(conditional_effects(mod1, effects="age",categorical = T))
once. This gives us the list of plots p
that we can then modify as desired.
Regarding the legend: It looks like conditional_effects
generates plot “D” (in your example) with both colour and fill aesthetics. To keep those legends combined as a single legend, they both need to have the same name
and the same labels
. So, in your example, it would probably be:
# Create list of plots
p = plot(conditional_effects(mod1, effects="age",categorical = T))
p[[4]] = p[[4]] +
scale_colour_discrete(name="Sentiment",
labels=c("Negative", "Neutral", "Positive")) +
scale_fill_discrete(name="Sentiment",
labels=c("Negative", "Neutral", "Positive"))
Also, regarding axis titles, in your example labs(x="Age")
sets the x-axis title to “Age”, but then theme(axis.title.x=element_blank())
removes the x-axis title.