This is my code:
I use the wine data from UCI Machine kearning.
This is the structure of dataBase "clase1".
class : num 1 1 1 1 1 1 1 1 1 1 ...
Alcohol : num 14.2 13.2 13.2 14.4 13.2 ...
Malic acid : num 1.71 1.78 2.36 1.95 2.59 1.76 1.87 2.15 1.64 1.35 ...
Ash : num 2.43 2.14 2.67 2.5 2.87 2.45 2.45 2.61 2.17 2.27 ...
Alcalinity of ash : num 15.6 11.2 18.6 16.8 21 15.2 14.6 17.6 14 16 ...
Magnesium : num 127 100 101 113 118 112 96 121 97 98 ...
Total phenols : num 2.8 2.65 2.8 3.85 2.8 3.27 2.5 2.6 2.8 2.98 ...
Flavanoids : num 3.06 2.76 3.24 3.49 2.69 3.39 2.52 2.51 2.98 3.15 ...
Nonflavanoid phenols : num 0.28 0.26 0.3 0.24 0.39 0.34 0.3 0.31 0.29 0.22 ...
Proanthocyanins : num 2.29 1.28 2.81 2.18 1.82 1.97 1.98 1.25 1.98 1.85 ...
Color intensity : num 5.64 4.38 5.68 7.8 4.32 6.75 5.25 5.05 5.2 7.22 ...
Hue : num 1.04 1.05 1.03 0.86 1.04 1.05 1.02 1.06 1.08 1.01 ...
OD280/OD315 of diluted wines: num 3.92 3.4 3.17 3.45 2.93 2.85 3.58 3.58 2.85 3.55 ...
Proline : num 1065 1050 1185 1480 735 ..
I made a function to generate histograms for every factor:
Histogram <- function(columnName) {
DFPlot <- ggplot(data=clase1, aes(x=clase1[columnName]))
DFPlot + geom_histogram(bins = 10)
}
Histogram("Color intensity")
Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?
Don't undertand why, because all values in clase1 are numeric.
please help
First of all, try to put your question into reprex:
Why reprex?
Getting unstuck is hard. Your first step here is usually to create a reprex, or reproducible example. The goal of a reprex is to package your code, and information about your problem so that others can run it and feel your pain. Then, hopefully, folks can more easily provide a solution.
What's in a Reproducible Example?
Parts of a reproducible example:
background information - Describe what you are trying to do. What have you already done?
complete set up - include any library() calls and data to reproduce your issue.
data for a reprex: Here's a discussion on setting up data for a reprex
make it run - include the minimal code required to reproduce your error on the dataβ¦
Second, what you want to get is achievable, but you need to understand how ggplot2 works. Namely, when you specify your aesthetics (aes
in your code) you are supposed to provide name without quotes since aes
is a quoting function (e.g., ggplot(df, aes(x = some_name))
and not (ggplot(df, aes(x = "some_name"))
). So, to get the behavior that you want you can do the following:
library(tidyverse)
#> ββ Attaching packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ tidyverse 1.2.1 ββ
#> β ggplot2 2.2.1 β purrr 0.2.4
#> β tibble 1.4.2 β dplyr 0.7.4
#> β tidyr 0.8.0 β stringr 1.3.0
#> β readr 1.1.1 β forcats 0.3.0
#> ββ Conflicts ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ tidyverse_conflicts() ββ
#> β dplyr::filter() masks stats::filter()
#> β dplyr::lag() masks stats::lag()
df <- tibble::tibble(x = rnorm(100), y = rnorm(100), z = rnorm(100))
Histogram <- function(columnName) {
DFPlot <- ggplot(data = df, aes_string(x = columnName))
DFPlot + geom_histogram(bins = 10)
}
Histogram("x")
1 Like