0

Data

I am trying to graph the above data set where the sessions are the x-axis, the y-axis is mean and there is a different for each equation over the weeks, which is indicated in the legend. I am unable to generate a graph and have tried a few different options:

ggplot(meant41, aes(x=Group.2, y=Q1, group=1)) + 
geom_line(aes(y=Q2)) +
geom_line(aes(y=Q3)) +
geom_line(aes(y=Q4)) +
geom_line(aes(y=Q5)) +
geom_line(aes(y=Q6)) +
geom_line(aes(y=Q7)) +
geom_line(aes(y=Q8)) +
geom_line(aes(y=Q9)) +
geom_line(aes(y=Q10)) +
geom_line(aes(y=Q11)) +
geom_point(size=3, shape=21, fill="black") 
xlab("Session") +
ylab("Mean") +
scale_colour_hue(name="Adherence Criteria",    # Legend label, use darker colors
               breaks=c("Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9", "Q10", "Q11"),
               labels=c("Hierarchy", "Dialectics", "Dialectical Modeling", "Reciprocal Communicaton",
                        "Irreverent", "V2", "V5", "Behavioral Analysis", "Insight", "Solution 
                         Analysis",
                        "Session Ending Strategies"),
               l=40) +                    # Use darker colors, lightness=40
ggtitle("Mean Therapist Adherencefect over Session") +
expand_limits(y=0) +                        # Expand y range
scale_y_continuous(breaks=0:20*.5) +         # Set tick every .5
theme_bw() +
theme(legend.justification=c(1,0),
    legend.position=c(1,0))               # Position legend in bottom right

`

sjdoc
  • 1
  • 2
  • link to the Data seems to be wrong – Vasily A Oct 08 '20 at 03:13
  • Take a look at some ggplot tutorials—the package doc website is very thorough—to see that the normal paradigm is to use long-shaped data and map variables to visual encodings such as color. You generally shouldn't be making repeat calls to a function like `geom_line` that serve essentially the same purpose—instead your data should be shaped in a way that you can assign those lines to different colors, etc – camille Oct 08 '20 at 13:30
  • Does this answer your question? [Plot multiple lines in one graph](https://stackoverflow.com/questions/17150183/plot-multiple-lines-in-one-graph) – camille Oct 08 '20 at 13:32
  • Also see https://stackoverflow.com/q/3777174/5325862 – camille Oct 08 '20 at 13:40

1 Answers1

0

I think you may need to rephrase the problem. You need to convert your dataframe from wide to long format, ie. instead of individual columns Q1-Q12, make a single (factor) column that has the Q1-Q12 labels and a separate values column. Then you can split the lines out in ggplot by colour, eg.

ggplot(meant41, aes(x=Group.2, y=ValueColumn, color=Q)) + 
geom_line()

To get the long format, have a look at tidyr:gather, something like:

new_df <- gather(meant41,Q,ValueColumn,Q1:Q12,factor_key=TRUE)

Good luck. Reference: http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/

Emanuel V
  • 143
  • 8