Reputation: 7526
I have a dataset with two different variables - city and town
When I plot them and add two regression lines with stat_smooth
, the legend symbols do not appear correctly (they appear as two 'a's):
ggplot(metrics, aes(x=popDensity, y= TPB, color = factor(type))) + geom_point() +theme_minimal() + stat_smooth(method = "lm", se = FALSE) +
geom_label_repel(aes(label= rownames(metrics)), size=3) +
theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=12)) +
labs(x = expression(paste( "density ", km^{2})), y = expression(paste("rating[![enter image description here][1]][1]")))+
theme(legend.position="top", legend.direction="horizontal")
However, when I remove the geom_label_repel
function, I get the legend symbols I need - but of course the labels do not appear.
ggplot(metrics, aes(x=popDensity, y= TPB, color = factor(type))) + geom_point() +theme_minimal() + stat_smooth(method = "lm", se = FALSE) +
#geom_label_repel(aes(label= rownames(metrics)), size=3) +
theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=12)) +
labs(x = expression(paste( "density ", km^{2})), y = expression(paste("rating")))+
theme(legend.position="top", legend.direction="horizontal")
Why does this happen and is there a known workaround for this issue? Additionally, is there a way to manually change the title of the legend title? I have tried using + theme(legend.title = "title"
) but get the error :
Error in (function (el, elname) :
Element legend.title must be a element_text object.
the sample data:
> dput(metrics)
structure(list(popDensity = c(4308, 27812, 4447, 5334, 4662,
2890, 24623, 5847, 1689, 481, 4100), TPB = c(1, 0.5, 1, 1.3,
0.8, 4, 0.2, 0.7, 5, 4, 2), type = c("City", "City", "City",
"City", "City", "City", "Town", "Town", "Town", "Town", "Town"
)), .Names = c("popDensity", "TPB", "type"), row.names = c("City1",
"City2", "City3", "City4", "City5", "City6", "Town1", "Town2",
"Town3", "Town4", "Town5"), class = "data.frame")
Upvotes: 0
Views: 2423
Reputation: 18445
Add show.legend = FALSE
within your geom_label_repel
call. The a's
seem to be the standard legend for the labels, which overrides the geom_point
that uses the same colours.
Upvotes: 1