0
$\begingroup$

Context: I am trying to change the legend labels for the Indices variable which contains "Positive" and "Negative" in "d_posneg" data frame.

enter image description here

Problem: However, my attempts have not yet worked. At present this is the code line that I am attempting to rename labels with in the graph below (line 6 of the ggplot):

scale_fill_discrete(name = "Indices", labels = c("Positive Emotion", "Negative Emotion")) +

Question: Does anyone know how to solve this?

See attached file for plot enter image description here

and code below for more context:

lines <- c("Positive" = "solid", "Negative" = "dotted")

fig1_1 <- d_posneg %>% # plot `posemo` and `negemo`
  ggplot(mapping = aes(x=year, y=rel_freq, linetype=factor(Indices))) +
  geom_line(alpha=1, size=0.7) +
  theme_classic() +
  scale_linetype_manual(values=lines) +
  scale_fill_discrete(name = "Indices", labels = c("Positive Emotion", "Negative Emotion")) +
  labs(x="Year", y="Positive Emotion and Negative Emotion Index") +
  scale_x_continuous(breaks=seq(1970,2017,2)) +
  expand_limits(y=1.5) +
  scale_y_continuous(breaks=seq(0.0,3, 0.1)) +
  theme(axis.text.x=element_text(angle=45, hjust=1)) +
  theme(legend.position=c(0.15,0.75)) 
print(fig1_1) 
$\endgroup$

1 Answer 1

1
$\begingroup$

Solution = move all of the arguments to scale_linetype_manual():


lines <- c("Positive" = "solid", "Negative" = "dotted")

fig1 <- d_posneg %>% # plot `posemo` and `negemo`
  ggplot(mapping = aes(x=year, y=rel_freq, linetype=factor(Indices))) +
  geom_line(alpha=1, size=0.7) +
  theme_classic() +
  scale_linetype_manual(values=lines, name="Indices", labels=c("Positive Emotion", "Negative Emotion")) +
  labs(x="Year", y="Positive Emotion and Negative Emotion Index") +
  scale_x_continuous(breaks=seq(1970,2017,2)) +
  expand_limits(y=1.5) +
  scale_y_continuous(breaks=seq(0.0,3, 0.1)) +
  theme(axis.text.x=element_text(angle=45, hjust=1)) +
  theme(legend.position=c(0.15,0.75))
print(fig1) 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.