Maria, take a look at the results of your reprex. Do you know why you're getting those errors instead of a plot?

Hello, I try to create a histogram. But it show me an error. I don't know what problem about my code. Is that I use the wrong form about ggplot()? What could I fix it?

library(tidyverse)
diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
just_height <- diabetes[, "height"]
ggplot(just_height, aes(x = height)) + 
  geom_histogram()
#> Error: `data` must be a data frame, or other object coercible by `fortify()`, not an integer vector.

Created on 2021-09-26 by the reprex package (v2.0.1)

Hello! I am trying to put together a histogram which correlates height with diabetes, but keep gettig errors telling me my data can not be an integer vector. How do I change the data into an acceptible format?

diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
just_height <- diabetes[, "height"]
ggplot(just_height, aes(x = height)) +
geom_histogram()

Error: data must be a data frame, or other object coercible by fortify(), not an integer vector.

Hello, I am trying to plot a histogram in R using a variable in my dataset. However, the plot appears with 30 bins and some spaces, which I understand as there is no value within this range/bin. Thus, I've tried to change the number of bins to 10 but, still getting this message stat_bin() using bins = 30. Pick better value with binwidth, and the plot does not change at all.

Please advise how should I revise my code to get different bins number? Many thanks!

Below is my code with bins = 10:

# Load tidyverse
library(tidyverse)

# Read diabetes.csv file into a data frame
diabetes <- read_csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")

# Plot ahistogram for the heights variable inside the diabetes data frame
just_height <- data.frame(drop_na(diabetes[, "height"]))

ggplot(just_height, aes(x = height), bins = 10) +
  geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

image

library(ggplot2)
diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
ggplot(diabetes, aes(x = age, y = chol, group = gender, color = gender)) +
geom_line()

image

Trying to use reprex package for the codes but every time when I do reprex selection, it crashes my R session. Any ideas why?

@Evan_Malkin, when I try your code on my machine, I get a different error:

diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
just_height <- diabetes[, "height"]
ggplot(just_height, aes(x = height)) +
  geom_histogram()
#> Error in ggplot(just_height, aes(x = height)): could not find function "ggplot"

It seems like you did not use reprex to create your example. What is missing from your code that would help your reprex? In other words, why am I getting a different error than you when I run your code?

@carmen2021 I haven't seen that happen. If you can, please come to one of our office hours or contact me directly and we'll see if we can get you sorted.

Help!

I am trying to create a histogram of height using ggplot. I got an error message saying that my data can not be an integer variable. I think I need to change my data in order to fit the parameters for the ggplot function, but I'm not sure how to do that. How do I change my data into a data frame?

diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
just_height <- diabetes[, "height"]
ggplot(just_height, aes(x = height)) + 
  geom_histogram()
#> Error: `data` must be a data frame, or other object coercible by `fortify()`, not an integer vector.
  1. Hi everyone. I expect this code to create a histogram of only the height variable using the diabetes data set. When I run this code exactly as is, the error I get is that ‘data’ must be a data frame, or other object coercible by ‘fortify()’, not an integer vector.
diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
just_height <- diabetes[, "height"]
ggplot(just_height, aes(x = height)) + 
  geom_histogram()
  1. Prior to adding the ggplot line, the code runs smoothly. I believe when running the ggplot, the error is that we put just_height, which is our new variable, as the dataset we are reading for our ggplot. Our dataset should be diabetes, and then our x variable should be just_height. Also, library(reprex) should be at the beginning of the code so that whoever is using this can reproduce this example.
library(reprex)
diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
just_height <- diabetes[, "height"]
ggplot(diabetes, aes(x = just_height)) + 
  geom_histogram()

Hello, can anyone help me with this question? I expect to create a histogram of certain column in a dataset by using ggplot.

library(ggplot2)
chickweight <- datasets::ChickWeight
ggplot(chickweight, aes(x=weight)) +
geom_histogram(color="black", fill="white",bins = 30)

Hello,
I expect this code to create a histogram of the temperature variable in the airquality dataset in base R.
Particularly, I want Temp on the x-axis and count on the y-axis.
When I run this code, R gives me the following output:
#Error: data must be a data frame, or other object coercible by fortify(), not an integer vector.
Can anyone please show me how to fix my code in order to get a histogram with Temp as my variable of interest?

library(tidyverse)
just_Temp <- airquality[,"Temp"]
ggplot(just_Temp, aes(x = Temp)) + 
  geom_histogram()
#> Error: `data` must be a data frame, or other object coercible by `fortify()`, not an integer vector.

Created on 2021-09-29 by the reprex package (v2.0.1)

Hello R Geeks, i am trying to get a histogram with a comparison of weight and cholesterol levels but I keep getting an error message could someone help me with getting the correct code. See below the error message am getting,

diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
just_height <- diabetes[, "height"]
ggplot(just_height, aes(x = height)) +geom_histogram()

*Error: stat_bin() can only have an x or y aesthetic. Run rlang::last_error() to see where the error occurred.

Thank you for your help.

Help! I am trying to change the intervals of the Y-axis. The graphs are being displayed by increments of 300 making the data challenging to analyze visually. Please let me know if there is a way to improve the visualization of this data.

Thanks,
Samantha

ggplot(diabetes, aes(age , chol)) +
  geom_point()+
  geom_smooth()+
  facet_grid(rows = vars(gender), cols = vars(frame))

#> Error in ggplot(diabetes, aes(age, chol)): could not find function "ggplot"

Created on 2021-09-29 by the reprex package (v2.0.1)

Hello R Community! I could use some help.

I'm trying to create a histogram using the code below; however, I'm getting the below error when attempting to use the ggplot function. Any help you can provide would be greatly appreciated - thanks in advance!

diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
just_height <- diabetes[, "height"]
ggplot(just_height, aes(x = height)) + 
  geom_histogram()
#> Error in ggplot(just_height, aes(x = height)): could not find function "ggplot"

Created on 2021-09-29 by the reprex package (v2.0.1)

Hello,

I'm trying to run this code to visualize weight, hip and gender with the diabetes data set using the ggplot2 function. How do I change the code to reflect a bar graph?

ggplot(data = diabetes, mapping = aes(x = weight, y = hip, color = gender)) +
  geom_point()
#> Error in ggplot(data = diabetes, mapping = aes(x = weight, y = hip, color = gender)): could not find function "ggplot"

Thanks,
Sherry

Hi All, I attached some code which has an error and I would like some help on how to correct it. Looks like my ggplot isn't working to make a histogram on diabetes. It says that data should be a data frame and not an integer vector. How do I go about changing that?

library(tidyverse)
diabetes <- read.csv("https://raw.githubusercontent.com/malcolmbarrett/au-stats412-612-01-reading_data/master/diabetes.csv")
just_height <- diabetes[, "height"]
ggplot(just_height, aes(x = height)) +
geom_histogram()
#> Error: data must be a data frame, or other object coercible by fortify(), not an integer vector.

Hello,

I wanted to create a histogram with the data for average height for women in the US. I first used the code below and it gave me a nice and basic visualization.
library(tidyverse)
library(reprex)
data()
women
ggplot(data = women) +
geom_point(mapping = aes(x = height, y = weight))
However, upon running the code below for the histogram, I get an error and am unable to proceed. Could someone please help me create a histogram with height being the x variable? Thanks so much.
ggplot(women, aes(x = height) +
geom_histogram()

The error I'm getting is this:
geom_bar: na.rm = FALSE, orientation = NA
stat_bin: binwidth = NULL, bins = NULL, na.rm = FALSE, orientation = NA, pad = FALSE
position_stack

Thanks, everyone, for attempting reprexes, and welcome to RStudio Community! If you're still confused about reprexes, please review this week's live session for an in-depth discussion.

Now, to fix the code!

It turns out that the real issue is a quirk of base R. read.csv() returns a regular data frame, and when we try to subset a single column in a data frame, R converts the object to a vector:

x <- data.frame(a = 1:5)

x
#>   a
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5

x[, "a"]
#> [1] 1 2 3 4 5

is.data.frame(x[, "a"])
#> [1] FALSE

ggplot() expects a data frame, not a vector. When you're dealing with base R, the solution is to set drop = FALSE, which keeps x as a data frame.

x[, "a", drop = FALSE]
#>   a
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5

is.data.frame(x[, "a", drop = FALSE])
#> [1] TRUE

Using this approach will fix our issue. I don't actually need the diabetes dataset here, so instead, I'll use the built-in cars dataset and make a histogram of the speed variable.

library(ggplot2)
just_speed <- cars[, "speed", drop = FALSE]
ggplot(just_speed, aes(x = speed)) + 
  geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Notably, the original code used read.csv(), which returns a regular data frame, but readr::read_csv() returns a tibble, a special case of the data frame. Tibbles don't have this behavior, and subsetting them always returns a tibble:

library(tidyverse)
y <- tibble(a = 1:5)
y[, "a"]
#> # A tibble: 5 x 1
#>       a
#>   <int>
#> 1     1
#> 2     2
#> 3     3
#> 4     4
#> 5     5

Using a tibble also solves our problem:

library(tidyverse)
cars <- as_tibble(cars)
just_speed <- cars[, "speed"]
ggplot(just_speed, aes(x = speed)) + 
  geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

This is the most wholesome thread ever :orange_heart:

2 Likes

This topic was automatically closed 7 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.