-1

I would like to plot a geom_point() using ggplot2.

My tibble is shown below. It is a single row tibble, and I would like the x axis to be the row and the y axis to be the column names (used as a date)

this

So I tried this :

g <- ggplot(data, aes(x = data, y = str_remove_all(colnames(data), "X"))) +
  geom_point()
g

But it does not work.

I have the idea of maybe transpose my tibble but I really do not know how to do it.

Thanks in advance for your help.

chemdork123
  • 12,369
  • 2
  • 16
  • 32
hitaton
  • 103
  • 2
  • 15
  • Yes, you need to reformat your data. Start by having a look here: https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format. You want a two column data.frame, where one column has the dates and the other column has the y axis values. – Axeman Aug 12 '20 at 18:25

1 Answers1

0

OP, you need to reorganize your data to follow Tidy Data guidelines. Luckily, there's a lot of ways to do this in R. I usually default to using tidyr and dplyr via gather(), where you need to "gather" all the columns together and use those column names as "key" and the "value" would be the observations (the row values).

Since OP did not share their data in a format that is easy to work with.. here's a reprex:

set.seed(1234)
df <- data.frame(NA)
for(i in LETTERS){ df[[i]]=sample(1:10, size=1) }
df <- df[,-1]   # gives you data frame with 1 row and 26 columns

That gives you a data frame with 26 columns ("A" through "Z"), where one observation for each column is a random integer from 1 to 10. Here's how to gather them together and use to make a plot:

library(dplyr)
library(tidyr)
library(ggplot2)

df <- df %>% gather(key=y, value=x)  # result = data frame with 2 columns and 26 rows

ggplot(df, aes(x=x, y=y)) + geom_point()

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32