Height' Must Be a Vector or a Matrix. Barplot Error

height' must be a vector or a matrix. barplot error

You can specify the two variables you want to plot rather than passing the whole data frame, like so:

data <- structure(list(V1 = c(34.88372093, 35.07751938, 35.27131783, 35.46511628, 35.65891473, 35.85271318), 
V2 = c(0.00029997, 0.00019998, 0.00029997, 0.00029997, 0.00069993, 0.00069993)),
.Names = c("V1", "V2"), row.names = c(NA, 6L), class = "data.frame")

barplot(data$V2, data$V1, xlab="Percentage", ylab="Proportion")

base r graphics version

Alternatively, you can use ggplot to do this:

library(ggplot2)
ggplot(data, aes(x=V1, y=V2)) + geom_bar(stat="identity") +
labs(x="Percentage", y="Proportion")

ggplot version

R: Barplot height must be a vector or matrix

I think the issue might be that you are trying to pass a DataFrame as the height parameter. This should only take a vector or a matrix. You can try something like this:

barplot(as.matrix(data), beside=T , legend.text=T, col=c("red" , "green", 
"blue"), ylim=c(0,140), ylab="height")

Hopefully this helps you out.

Plotting a simple bargraph in R, Error: Height must be a vector of matrix

R cannot tell which variable is which when you pass it an entire data.frame to plot. In this case, the error, 'height' must be a vector or a matrix is telling you you didn't give the plot function what it wanted.

For a barplot, using ?barplot will tell you what is needed:

barplot(height, width = 1, space = NULL,
names.arg = NULL ....

In your case, you have a data.frame with heights in the second column, and names in the first, so you want to do:

barplot(data[ ,2], names.arg = data[ ,1])

Grouped bar plot in R - 'height' must be a vector or a matrix

  library(tidyverse)
df <- tribble(~location, ~total_deaths_per_million, ~total_tests_per_thousand,
"Albania" , 617, 150,
"Denmark" , 407, 2818,
"Germany" , 837, 524,
"Russia" , 578, 756)

df2 <- df %>%
pivot_longer(-location)
barplot(value ~ name + location, data = df2,
beside = TRUE)

Sample Image

df %>% 
pivot_longer(-location) %>%
ggplot(aes(location, value, fill = name)) +
geom_col(position = "dodge")

Sample Image

df %>% 
pivot_longer(-location) %>%
ggplot(aes(name, value)) +
geom_col(position = "dodge") +
facet_wrap(~location) + coord_flip()

Sample Image

Barplot Error in R

Ah, that solves the problem : you have 3 dimensions in your table, barplot can't deal with that. Take the 2 columns you want to use for the barplot function, eg:

# sample data
Df <- data.frame(
TIME = as.factor(seq.Date(as.Date("2010-09-11"),as.Date("2010-09-20"),by="day")),
FOCUS.APP = as.factor(rep(c("F101","F102"),5)),
X = sample(c(TRUE,FALSE,NA),10,r=T)
)

# make tables
T1 <- table(Df)
T2 <- table(Df[,-3])

# plot tables
barplot(T1)
barplot(T2)

This said, that plot must look interesting to say the least. I don't know what you try to do, but I'd say that you might to reconsider your approach to it.

R: Converting tables to data frame: 'height' must be a vector or a matrix

Use the formula method for data.frame

t <- as.data.frame(t)
color <- c("red", "black")
color_1 <- sample(color, nrow(t), replace=TRUE, prob=c(0.5, 0.5))
barplot(Freq ~ n, data = t, col = color_1, border = NA)

-output

Sample Image



Related Topics



Leave a reply



Submit