棒棒糖图的多面包装 [英] Facet wrap of a lollipop plot
问题描述
我正在尝试使用多面包装制作多个棒棒糖图,如第三个代码块/示例和下面
但是,我无法使代码示例正常工作.您能帮我看看哪里写错了吗(如果有的话)?
数据:
set.seed(1)数据< -as.data.frame(矩阵(sample(2:20,40,replace = T),ncol = 10))colnames(data)<-c("math","english","biology","music","R-coding","data-viz","french","french",物理",统计",运动")数据< -rbind(rep(20,10),rep(0,10),数据)行名(数据)<-c(-",-","John","Angli","Baptiste","Alfred")
当我运行代码来绘制它时:
#条形图数据<-数据%>%slice(c(3:6))%&%t()%&%;%as.data.frame()%&%;%add_rownames()%>%排列(V1)%mutate(rowname = factor(rowname,rowname))%&%;%收集(键=名称,值=标记,-1)#重新编码data $ name<-重新编码(data $ name,V1 ="John",V2 ="Angli",V3 ="Baptiste",V4 ="Alfred")# 阴谋数据%>%ggplot(aes(x = rowname,y = mark))+geom_bar(stat ="identity",fill =#69b3a2",width = 0.6)+coord_flip()+theme_ipsum()+主题(panel.grid.minor.y = element_blank(),panel.grid.major.y = element_blank(),axis.text = element_text(size = 48))+ylim(0,20)+ylab(标记")+xlab(")+facet_wrap(〜name,ncol = 4)
我收到以下错误:
订单(V1)中出现错误:找不到对象'V1'
链接页面上的前两个代码块依赖于两个列标记,因此我认为第一个向量自动命名为V1.但是我不清楚如何解决此示例代码,因为这些列已被命名.我希望这个问题清楚.
列名保持不变,因此那里没有 V1
和 V2
列.您也可以将 gather
替换为 pivot_longer
.试试:
库(tidyverse)数据<-数据%>%切片(3:6)%>%t()%&%;%as.data.frame()%&%;%add_rownames()%>%mutate(rowname = factor(rowname,rowname))%&%;%ivot_longer(cols = -1,values_to ='mark')数据%>%ggplot(aes(x = rowname,y = mark))+geom_bar(stat ="identity",fill =#69b3a2",width = 0.6)+coord_flip()+#theme_ipsum()+主题(panel.grid.minor.y = element_blank(),panel.grid.major.y = element_blank())+ylim(0,20)+ylab(标记")+xlab(")+facet_wrap(〜name,ncol = 4)
I'm trying to make multiple lollipop plots using a facet wrap, like in the third code block / example and picture below on this page.
However, I can't get the code example to work. Can you please help me see where it is written incorrectly (if at all)?
The data:
set.seed(1)
data <-as.data.frame(matrix( sample( 2:20 , 40 , replace=T) , ncol=10))
colnames(data) <- c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )
data <-rbind(rep(20,10) , rep(0,10) , data)
rownames(data) <- c("-", "--", "John", "Angli", "Baptiste", "Alfred")
When I run the code to plot it:
# Barplot
data <- data %>% slice(c(3:6)) %>%
t() %>%
as.data.frame() %>%
add_rownames() %>%
arrange(V1) %>%
mutate(rowname=factor(rowname, rowname)) %>%
gather(key=name, value=mark, -1)
#Recode
data$name <- recode(data$name, V1 = "John", V2 = "Angli", V3 = "Baptiste", V4 = "Alfred")
# Plot
data %>% ggplot( aes(x=rowname, y=mark)) +
geom_bar(stat="identity", fill="#69b3a2", width=0.6) +
coord_flip() +
theme_ipsum() +
theme(
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
axis.text = element_text( size=48 )
) +
ylim(0,20) +
ylab("mark") +
xlab("") +
facet_wrap(~name, ncol=4)
I get the following error:
Error in order(V1) : object 'V1' not found
The first two code blocks on the linked page rely on two column tibbles, so I think the first vector automatically gets named V1. But it's not clear to me how to fix this example code, since the columns are named already. I hope this question is clear.
The column names do remain intact so you don't have V1
, V2
columns there. Also you can replace gather
with pivot_longer
. Try :
library(tidyverse)
data <- data %>%
slice(3:6) %>%
t() %>%
as.data.frame() %>%
add_rownames() %>%
mutate(rowname=factor(rowname, rowname)) %>%
pivot_longer(cols = -1, values_to = 'mark')
data %>% ggplot( aes(x=rowname, y=mark)) +
geom_bar(stat="identity", fill="#69b3a2", width=0.6) +
coord_flip() +
#theme_ipsum() +
theme(
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank()
) +
ylim(0,20) +
ylab("mark") +
xlab("") +
facet_wrap(~name, ncol=4)
这篇关于棒棒糖图的多面包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!