Home > Blockchain >  How to plot only one column in ggplot?
How to plot only one column in ggplot?

Time:03-01

Given this simple data I want to plot the equivalent of base

plot(dat$value)

but with ggplot.

 dat= structure(list(name = c("Cord", "Cord", 
"amo", "amo", 
 "amo", "ramo"), 
value = c(7, 0.7, 9, 
0.9, 0.8, 0.7)), row.names = c(NA, 
 6L), class = "data.frame")

I tried:

> ggplot(data = dat)        geom_point(aes(x = value, colour = name)) 
 Error in `check_required_aesthetics()`:
 ! geom_point requires the following missing aesthetics: y

I need to plot "count" on y axis vs value on x axis

CodePudding user response:

If you are wanting count, then you can use stat_count, which will then give you a y value to plot, and you can also specify that you want point.

library(tidyverse)

ggplot(dat, aes(x = value, colour = name))  
  stat_count(geom = "point")

Output

enter image description here

CodePudding user response:

Base R plot(dat$value) has an implicit x equal to seq_along(dat$value).
Use x = seq_along(value) and y = value.

dat <- structure(list(name = c("Cord", "Cord", 
                               "amo", "amo", 
                               "amo", "ramo"), 
                      value = c(7, 0.7, 9, 
                                0.9, 0.8, 0.7)), 
                 row.names = c(NA, 6L), 
                 class = "data.frame")

library(ggplot2)

ggplot(dat, aes(x = seq_along(value), y = value, color = name))  
  geom_point()

Created on 2022-03-01 by the reprex package (v2.0.1)

  • Related