我打印了一个矩阵,我有两列我希望进入条形图,我不知道如何。当我尝试它时,它使用整个矩阵作为X值。这是我的代码
smoke <- matrix(c(53,42,40,40,39,34,34,30,28,24,22,21,20,16,'A','A','A','B','A','A','A','A','B','A','C','B','B', 'B'),nrow=14, ncol = 2)
colnames(smoke) <- c("NumberofBooks","Grade")
smoke <- as.table(smoke)
smoke
任何帮助绘制数字与字母的关系都将非常感谢!
答案 0 :(得分:0)
在我看来,将这些值放入条形图的最简单方法就是用数据框替换矩阵。就像这里一样:
number_of_books <- c(53,42,40,40,39,34,34,30,28,24,22,21,20,16)
grade <- c('A','A','A','B','A','A','A','A','B','A','C','B','B', 'B')
smoke <- data.frame(number_of_books,grade)
bar_graph <- ggplot(smoke, aes(number_of_books,grade)) + geom_bar(stat="identity")
希望它能帮到你。
答案 1 :(得分:0)
smoke <-
matrix(c(53,42,40,40,39,34,34,30,28,24,22,21,20,16,'A','A','A','B','A','A','A','A','B','A','C','B','B', 'B'),nrow=14, ncol = 2)
colnames(smoke) <- c("NumberofBooks","Grade")
smoke <- as.data.frame(smoke)
p <- ggplot(data = smoke, aes(y = NumberofBooks, x = Grade))
p <- (p
+ geom_bar(stat = 'Identity', position = 'dodge', color = 'white')
)
print(p)
不完全确定您要查找的内容,但这可以帮助您入门。您需要更改为data.frame才能使用ggplot
答案 2 :(得分:0)
这是否符合您的要求?
library(dplyr)
library(ggplot2)
# install.packages("dplyr"); install.packages("ggplot2")
smoke <- matrix(c(53,42,40,40,39,34,34,30,28,24,22,21,20,16,'A','A','A','B','A','A','A',' A','B','A','C','B','B', 'B'),nrow=14, ncol = 2)
colnames(smoke) <- c("NumberofBooks","Grade")
smoke %>%
as.data.frame() %>%
group_by(Grade) %>%
summarise(NumberofBooks = mean(as.numeric(NumberofBooks))) %>%
ggplot(aes(x = Grade, y = NumberofBooks)) +
geom_bar(stat = "identity") +
xlab("Grade") +
ylab("Average Number of Books") +
ggtitle("Average number of book by Grade")