Foxhunt
Foxhunt

Reputation: 870

R - Ggplot dataframe do not sort data properly

I am trying to plot data from a dataset

I used this function:

wine_red <- read.csv2("winequality-red.csv")
ggplot(wine_red, aes(x=quality, y=alcohol)) + 
    scale_fill_continuous(type = "viridis") +
    geom_bin2d(binwidth=1)

But doing this I am obtaining a y axis which is not properly sorted: it goes from 10 -> 19 then 7 -> 9

plot

How can I sort it in the correct numeric order?

Upvotes: 0

Views: 171

Answers (2)

Rfanatic
Rfanatic

Reputation: 2282

Your code is working fine just as data you need to use winequality_red and for the x=winequality_red$quality and y=winequality_red$alcohol. Also below an example how to convert your data, to delimit the columns.

winequality_red <- read_delim("HERE SPECIFY YOUR DATA LOCATION", 
  ";", escape_double = FALSE, trim_ws = TRUE)

ggplot(winequality_red , aes(x=winequality_red$quality, y=winequality_red$alcohol)) + 
  scale_fill_continuous(type = "viridis") +
  geom_bin2d(binwidth=1)

enter image description here

Upvotes: 1

TarJae
TarJae

Reputation: 78917

Something like this?

library(tidyverse)

df <- winequality_red
df %>% 
  select(quality, alcohol) %>% 
  ggplot(aes(x = quality, y=alcohol))+
  scale_fill_continuous(type = "viridis")+
  geom_bin2d(binwidth = 1)

enter image description here

Upvotes: 1

Related Questions