0
$\begingroup$

Context: I have two variables under emotion_dict that I am graphing in the same line graph.

Problem: However when I change the linetype in geom_line, it changes the appearance of both variables.

Question: Does anyone know how to alter the code below to keep the line types separate and display a key? I have done this before only with the ggline function, but that is not appropriate here.

See attached file and code below for more context:enter image description here

posneg_plot2 <- d_posneg %>%
  ggplot(mapping = aes(x=year, y=rel_freq, group=emotion_dict, colour=emotion_dict)) +
  geom_line(alpha = 1, size=0.7, linetype=2, colour="black") +
  theme_light() +
  labs(x="Year", y="Positive and Negative Sentiment (%)") +
  scale_x_continuous(breaks=seq(1970,2017,2)) +
  theme(axis.text.x=element_text(angle=45, hjust=1)) +
  scale_color_hue(labels = c("Negative Sentiment", "Positive Sentiment")) +
  labs(colour = "LIWC Dictionaries") + theme(legend.position = "bottom") +
  theme(text=element_text(family="Times New Roman", size=17))
  #stat_cor(, method = "pearson", p.accuracy = 0.001, r.accuracy = 00.01, size = 4.5, colour = "black") 

EDIT: Answer below and now attempting to change the labels within the group accordingly: enter image description here

$\endgroup$

1 Answer 1

2
$\begingroup$

You should be able to simply specify the field to be used for the linetype for the linetype argument within an aes mapping as follows:

posneg_plot2 <- d_posneg %>%
  ggplot(mapping = aes(x=year, y=rel_freq, group=emotion_dict, colour=emotion_dict)) +
  geom_line(aes(linetype=emotion_dict), alpha = 1, size=0.7, colour="black")
$\endgroup$
6
  • $\begingroup$ Thank you @Oxbowerce! Would you know why/ how to change the label names? I am trying the below but it hasn't yet changed the labels: {r} labs(color = "LIWC Dictionaries") + scale_color_hue(labels = c("Negative Sentiment", "Positive Sentiment")) + I've also attached a new photo in the above comment. $\endgroup$
    – n.baes
    Dec 28, 2021 at 12:57
  • $\begingroup$ Which labels do you exactly mean? The labels in the legend or do you want to add labels next to the lines? $\endgroup$
    – Oxbowerce
    Dec 28, 2021 at 13:46
  • $\begingroup$ Apologies for not being clear @Oxbowerce! I mean the labels for the legend that is shown underneath "Year": emotion_dict negemo and posemo $\endgroup$
    – n.baes
    Dec 30, 2021 at 21:46
  • $\begingroup$ I think using scale_linetype_manual(labels=c("Negative Sentiment", "Positive Sentiment")) should do what you're looking for. $\endgroup$
    – Oxbowerce
    Dec 31, 2021 at 8:48
  • $\begingroup$ Hmm, I added it and this error arises: "Error: Insufficient values in manual scale. 2 needed but only 0 provided. Run rlang::last_error() to see where the error occurred." $\endgroup$
    – n.baes
    Jan 3 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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