0
votes

D Dear all, given the following dataframe, I am trying to plot the values for each column and marked overall (coloured) by type (there are three groups, alpha, beta, gamma).

In other words the x axis should display 9 points (x100,x110....,x180) and the y axis range should be from 0 to 2 (each column takes a value from 0 to 2 max).

Each of the resulting three lines should highlight each of the three categorical variables.

Apologies about the format of the dataframe, I have not figured out how to pretty output it yet.

structure(list(Group.1 = c("alpha", "beta", "gamma"), x100 = 
c(1.31729175522923, 
0.985278656706214, 0.156200287397951), x110 = c(1.54471416538581, 
0.915659603197128, 0.733224726747721), x120 = c(1.27778739808127, 
0.813037838321179, 0.779596480540931), x130 = c(1.25000598328188, 
0.488610395696014, 0.806707685347646), x140 = c(1.82296009687707, 
1.16132276877761, 1.31973652262241), x150 = c(0.929914232343435, 
1.41477890312672, 1.41652730805799), x160 = c(1.19612871715799, 
0.801679770927876, 0.39746836386621), x170 = c(1.88860023999587, 
1.03295020200312, 0.729622524231672), x180 = c(0.926427994389087, 
1.20304362708703, 1.57529754098505)), row.names = c(NA, -3L), 
class = "data.frame")

I am trying to use ggplot (any other method of plotting would do) but in reading the requirements of the ggplot function I am struggling to understand if I should possibly create a vector of values to use as the aes parameter y?

Thanks in advance,

F

2

2 Answers

0
votes

To get the layout you need for plotting you need to convert your data.frame to a long format using pivot_longer. From there you can just use ggplot as normal. To unstack the bars use position = dodge.

library(tidyverse)

data <- structure(list(Group.1 = c("alpha", "beta", "gamma"), 
               x100 = c(1.31729175522923, 0.985278656706214, 0.156200287397951), 
               x110 = c(1.54471416538581, 0.915659603197128, 0.733224726747721), 
               x120 = c(1.27778739808127, 0.813037838321179, 0.779596480540931), 
               x130 = c(1.25000598328188, 0.488610395696014, 0.806707685347646), 
               x140 = c(1.82296009687707, 1.16132276877761, 1.31973652262241), 
               x150 = c(0.929914232343435, 1.41477890312672, 1.41652730805799), 
               x160 = c(1.19612871715799, 0.801679770927876, 0.39746836386621), 
               x170 = c(1.88860023999587, 1.03295020200312, 0.729622524231672), 
               x180 = c(0.926427994389087, 1.20304362708703, 1.57529754098505)), 
          row.names = c(NA, -3L), 
          class = "data.frame")
data %>% 
  pivot_longer(cols = contains("x"),
               names_to = "data_points",
               values_to = "vals") %>% 
  ggplot(aes(x = data_points, y = vals, fill = Group.1)) +
  geom_col(position = "dodge")

Created on 2020-11-11 by the reprex package (v0.3.0)

-1
votes

A base-R solution would be to reshape the data to long format with reshape before plotting.

dflong <- reshape(df, direction = "long", idvar = "Group.1", timevar = "xval", 
                  varying = 2:10, v.names = "yval", times = colnames(df)[2:10])

library(ggplot2)
ggplot(data = dflong, aes(x = xval, y = yval, color = Group.1)) +
  geom_line(aes(group = Group.1))

enter image description here