0

I'm trying to make a histogram but I keep running into the an error message. Here is my code

library(readxl)

data <- read_excel("data.xls")

hist(data)

This is my sample. I want to create a histogram the y-axis be 0-100, x-axis (safely, basic, limited, etc) the numbers (39,29,8,12,12) be in the graph. Does this help make sense?

   Safely  Basic  Limited  Unimproved  Open

   39     29         8          12      12

Error in hist.default(data) : 'x' must be numeric

What am I doing wrong? I don't know understand the error message.

user438383
  • 5,716
  • 8
  • 28
  • 43
Ikamuzu
  • 11
  • 1
  • 1
  • 3
  • Welcome to StackOverflow! Please read [ask] and may take the [tour] as your question does not fit any of our guidelines. Additional, it appears that you did not put any effort in your question, then why should we put an effort into an answer? – Hille Apr 09 '19 at 11:46
  • 2
    In your code `data` is a dataframe (or, more precisely, a tbl). A histogram requires a vector of numeric values for its argument `x`, but you are passing a data frame to the `hist` function. Is there a certain variable in your data that you want to creat the histogram for? It would be helpful if you could provide a sample of the data in `data`. – Wil Apr 09 '19 at 11:52
  • 1
    How many columns does your data.frame has? Say you want to plot the histogram of column `1`. Then do `hist(data[[1]])`. And please [do not `attach` data](https://stackoverflow.com/questions/10067680/why-is-it-not-advisable-to-use-attach-in-r-and-what-should-i-use-instead). – Rui Barradas Apr 09 '19 at 11:57
  • i only one column – Ikamuzu Apr 09 '19 at 12:00
  • Are each of those names (Safety, Basic, Limited, etc) different columns in the dataset? For which variable do you want a histogram? – Wil Apr 09 '19 at 12:30
  • @Wil yes those are the names of the columns in the dataset. – Ikamuzu Apr 09 '19 at 12:37
  • which column do you want a histogram for? – Wil Apr 09 '19 at 12:37
  • how so? A [histogram](https://www.datacamp.com/community/tutorials/make-histogram-basic-r) is a plot of a single variable, not multiple variables. Or do you want a different histogram for each variable/column? – Wil Apr 09 '19 at 12:50
  • @Wil i have updated my post for further information. Sorry if I am confusing you. – Ikamuzu Apr 09 '19 at 12:55
  • I have added an answer below based on your edited post. – Wil Apr 09 '19 at 12:58
  • A histogram is NOT the same as a bar plot. You want to create a bar plot. – Roland Apr 09 '19 at 13:54

2 Answers2

3

In your case data is not a variable, but a dataframe that contains variables. You can take the histogram of each single variable like this:

library(readxl)
data <- read_excel("data.xls")

If you want to look at the histogram of variable Safely:

hist(data$Safely)

You can access each variable contained in data in the same way.

Leevo
  • 1,683
  • 2
  • 17
  • 34
3

The issue is that you are passing a dataframe to the hist() function, when it requires a vector for its argument x (see ?hist). Based on your edited post, you would want:

hist(as.numeric(data[1,]))

Where data[1,] creates a vector from the first row of your dataframe.

Though it seems like you may actually be looking for a bar plot. In that case, try:

plot_data <- data.frame(t(data)) %>% 
  tibble::rownames_to_column()

ggplot(plot_data,aes(x = rowname,y=t.data.)) + 
  stat_identity(geom = "bar")

From @user2554330, a simpler base graphics method:

f <- as.numeric(data[1,])
names(f) <- names(data)
barplot(f)
Wil
  • 3,076
  • 2
  • 12
  • 31
  • @user2554330 agreed, I was actually working on that edit as you made your comment - good point. My answer is ugly in order to get the labels, so if you have any thoughts on how to clean it up feel free. – Wil Apr 09 '19 at 13:08
  • I don't speak `gg` fluently, but here's a base graphics way: `f <- as.numeric(data[1,]); names(f) <- names(data); barplot(f)`. – user2554330 Apr 09 '19 at 13:11