I am trying to plot a graph of 2 x-axis variables using ggplot

I have a dataset for which I want to plot a graph with variables, "Choices group 1" and "Choices group 2) (same scale) on the x-axis while they have the same y-axis variable, "Impact. See my relevant columns below

               "Choices group 1" = c(1,  5, 7, 6, 8, 2, 4)
               "Choices group 2"=c(2, 4, 2, 3, 4, 9, 4)
               "Impact"=c(4,-2,5,6,8,0,-9) 

I want to plot the graph using ggplot to produce "impact" on the y-axis and "Choices group 1" and "Choices group 2" both on the x-axis (with their scale ranging from 1-10)

library(tidyverse)
(mydf <- tibble(
  "Choices group 1" = c(1, 5, 7, 6, 8, 2, 4),
  "Choices group 2" = c(2, 4, 2, 3, 4, 9, 4),
  "Impact" = c(4, -2, 5, 6, 8, 0, -9)
))

(mytidydf <- pivot_longer(mydf,
  cols = starts_with("Choices"),
  names_to = c("dummy1", "dummy2", "Choices Group"),
  names_sep = " ",
  values_to = "Choices"
) |>
  select(-starts_with("dummy")))

ggplot(
  data = mytidydf,
  aes(
    x = Choices,
    y = Impact,
    color = `Choices Group`
  )
) +
  geom_point() +
  scale_x_continuous(limits = c(1,10),
                     breaks = 1:10,
                     minor_breaks = NULL)

Thank you for sharing the codes above.

My dataset produces "NA" under the newly created "Choices Group" column, not sure why
I also wanted to ask if you deliberately left out "Choices group 1" and "Choices group 2" from the codes because I see "dummy 1" and "dummy 2"

I assumed that in the original data only two columns are named with text beginning "Choices" ; it seems this isn't true in your data.

yes, they add no value past that point; we have the groups 1 and 2 and we have a key telling us that they represent differently numbered Choices Groups

Yes I used a sample dataset to give me the required codes

However the actual columns are named "HLriskydecisions" and "IBriskydecisions", but I thought I could adapt the code you sent

You still need to pivot wider but its probably simpler as you dont have a group number to extract. So you wont need dummys etc.

Thank you

So if you don't mind could you help reproduce the wider format code again using my own columns "HLriskydecisions" and "IBriskydecisions"

The ggplot codes at the bottom are clear but I need to understand how to generate the "group" using the wide format

can you copy and paste the output of dput(head(yourdataframe,10))?

structure(list(timestamp = structure(c(1671202989.131, 1671205022.695,
1671205038.254, 1671205631.122, 1671205749.948, 1671205833.992,
1671205906.979, 1671206134.836, 1671206193.025, 1671206274.855
), tzone = "UTC", class = c("POSIXct", "POSIXt")), S/N = c(1,
2, 3, 4, 5, 6, 7, 8, 9, 10), gender = c("Man", "Man", "Woman",
"Man", "Man", "Man", "Woman", "Woman", "Man", "Man"), income = c("0kr – 100.000kr",
"0kr – 100.000kr", "100.000kr – 200.000kr", "0kr – 100.000kr",
"0kr – 100.000kr", "0kr – 100.000kr", "0kr – 100.000kr",
"0kr – 100.000kr", "100.000kr – 200.000kr", "0kr – 100.000kr"
), age = c(25, 26, 25, 23, 24, 26, 26, 23, 22, 24), g1q1 = c("A",
"A", "A", "A", "A", "B", "A", "A", "A", "A"), g1q2 = c("A", "A",
"A", "A", "A", "A", "A", "A", "A", "A"), g1q3 = c("B", "A", "A",
"A", "A", "A", "A", "A", "A", "A"), g1q4 = c("B", "A", "A", "A",
"A", "A", "A", "A", "A", "A"), g1q5 = c("B", "A", "A", "A", "B",
"A", "B", "B", "A", "B"), g1q6 = c("B", "A", "A", "A", "B", "A",
"B", "B", "A", "B"), g1q7 = c("B", "B", "B", "A", "B", "B", "B",
"B", "B", "B"), g1q8 = c("B", "B", "B", "B", "B", "B", "B", "B",
"B", "B"), g1q9 = c("B", "B", "B", "B", "B", "B", "B", "B", "B",
"B"), g1q10 = c("B", "B", "B", "B", "B", "B", "B", "B", "B",
"B"), g2 = c("0", "8, 9, 10, 11, 12", "4, 7, 10", "1, 2, 3, 4, 5, 6",
"1, 2, 11, 12", "3, 6, 10", "2, 4, 6, 8, 10, 12", "1, 3, 5, 6, 8, 9, 11",
"1, 2, 3, 4", "1, 2, 3, 4, 5, 6, 7, 8"), g3q1 = c("A", "A", "A",
"A", "B", "A", "B", "B", "B", "A"), g3q2 = c("A", "B", "B", "B",
"A", "B", "B", "A", "B", "B"), g3q3 = c("A", "A", "A", "A", "B",
"A", "A", "B", "B", "A"), ageGroup = c("0-29", "0-29", "0-29",
"0-29", "0-29", "0-29", "0-29", "0-29", "0-29", "0-29"), countg2 = c(0,
5, 3, 6, 4, 3, 6, 7, 4, 8), gendummy = c(1, 1, 0, 1, 1, 1, 0,
0, 1, 1), d1 = c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1), d2 = c(1, 1,
1, 1, 1, 1, 1, 1, 1, 1), d3 = c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1),
d4 = c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1), d5 = c(0, 1, 1, 1,
0, 1, 0, 0, 1, 0), d6 = c(0, 1, 1, 1, 0, 1, 0, 0, 1, 0),
d7 = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0), d8 = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0), d9 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
d910 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), HLriskydecisions = c(8,
4, 4, 3, 6, 5, 6, 6, 4, 6), IBriskydecisions = c(10, 5, 7,
4, 6, 7, 4, 3, 6, 2), Coefnume = c(12, 2, 6, 0, 4, 6, 0,
-2, 4, -4), coefdeno = c(12, 7, 9, 6, 8, 9, 6, 5, 8, 4),
r = c(1, 0.29, 0.67, 0, 0.5, 0.67, 0, -0.4, 0.5, -1)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))

I want to plot HLriskydecisions and IBriskydecisions on the x-axis and r on the y-axis as lines

library(tidyverse)
(mydf <- tibble(
  "abc" = c(1, 5, 7, 6, 8, 2, 4),
  "xyz" = c(2, 4, 2, 3, 4, 9, 4),
  "Impact" = c(4, -2, 5, 6, 8, 0, -9)
))

(mytidydf <- pivot_longer(mydf,
                          cols = c("abc","xyz"),
                          names_to = "group name I want",
                          values_to = "value label of my choice"))

ggplot(
  data = mytidydf,
  aes(
    x = `value label of my choice`,
    y = Impact,
    color = `group name I want`
  )
) +
  geom_point() +
  scale_x_continuous(limits = c(1,10),
                     breaks = 1:10,
                     minor_breaks = NULL)

Thank you so much. this works

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.