- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想生成一个facet_wrap,其中构面内因子的顺序是基于列因子顺序之一。问题的核心是每个组都有重复的因子水平,当我绘制时,facet_wrap
中仅正确排序了一个因子水平。 (见下图)
我尝试对每个组中的因子级别进行排序,并且每个因子级别应在每个方面内正确排序。
这是我的尝试
df_pattern<- data.frame(address = rep(rep(LETTERS[1:3]),3))
df_TP <- data.frame(No=rep(seq(1:3)),
clas=c("Good","Bad","Ugly"),stringsAsFactors = F)
set.seed(12)
df_ex <- df_pattern%>%
mutate(No=rep(seq(1:3),each=3))%>%
left_join(df_TP)%>%
mutate(clas=sample(clas))%>%
group_by(No)
# address No clas
# <fctr> <int> <chr>
# 1 A 1 Good
# 2 B 1 Ugly
# 3 C 1 Ugly
# 4 A 2 Good
# 5 B 2 Ugly
# 6 C 2 Bad
# 7 A 3 Bad
# 8 B 3 Bad
# 9 C 3 Good
现在让我们尝试根据用户定义的 clas 列顺序对地址级别进行排序
set.seed(12)
df_ex <- df_pattern%>%
mutate(No=rep(seq(1:3),each=3))%>%
left_join(df_TP)%>%
mutate(clas=sample(clas))%>%
group_by(No)%>%
mutate(clas=factor(clas,levels=c("Good","Bad","Ugly")))%>%
mutate(address=factor(address,levels=unique(address[order(clas)])))%>%
mutate(address=as.character(address))%>%
arrange(No,clas)
address No clas
# <fctr> <int> <ord>
# 1 A 1 Good
# 2 B 1 Ugly
# 3 C 1 Ugly
# 4 A 2 Good
# 5 C 2 Bad
# 6 B 2 Ugly
# 7 C 3 Good
# 8 A 3 Bad
# 9 B 3 Bad
正如您所看到的,图中只有 No=1
组排序正确。也许这是因为数据集中只有一个因素水平。
> levels(df_ex$address)
[1] "A" "B" "C"
我们如何对每组中的因子水平进行排序并在facet_wrap中显示它们?根据每个facet_wrap
中的class
级别?
谢谢!
ggplot代码
ggplot(df_ex, aes(x=address,y="",fill=clas)) + #x axis bias voltage dependence
geom_tile() +
scale_fill_manual(values=c('Good'="green","Bad"="Blue","Ugly"="black"))+
facet_wrap(~No,ncol=1,scales = "free_x")+
theme(legend.position = "top",axis.text.y = element_text(size = 20,angle = 90),axis.text.x = element_text(size=12,face="bold",colour = "black"),
axis.title.y = element_text(face="bold",size = 20, colour = "black"),
axis.title.x = element_text(face="bold",size = 20 , colour = "black"),
strip.text = element_text(size=26, face="bold"),
strip.background = element_rect(fill="#FFFF66", colour="black", size=0.5),
plot.title=element_text(face="bold",color="red",size=14),
legend.title = element_text(colour="black", size=26,face="bold"),
legend.text = element_text(colour="black", size=18))+
labs(x = "address",y = "")
最佳答案
这个老问题已经有 accepted answer 。但由于它被用作欺骗目标,我觉得有必要提出一个稍微改进且更简洁的变体。
它基于 ggplot2
包的最新增强功能,即 scale_x_discrete()
的 labels
参数,以及 Hadley 的 forcats
软件包于 2016 年 8 月发布到 CRAN。所提出的解决方案增强了 accepted answer使用 this answer 中的 Material .
df_ex
需要修改为包含一个变量,该变量保证所有方面的整体排序顺序:
library(dplyr) # version 0.5.0 used
df_ex <- df_ex %>% mutate(ordered = paste0(No, address) %>%
forcats::fct_inorder())
df_ex
的附加列现在如下所示:
address No clas ordered
<chr> <int> <fctr> <fctr>
1 A 1 Good 1A
2 B 1 Ugly 1B
3 C 1 Ugly 1C
4 A 2 Good 2A
5 C 2 Bad 2C
6 B 2 Ugly 2B
7 C 3 Good 3C
8 A 3 Bad 3A
9 B 3 Bad 3B
由于 df_ex
已使用 arrange()
按所需顺序排序,fct_inorder()
返回新列 ordered
关卡的顺序与首次出现的顺序相同。
x 轴上绘制的是 ordered
,而不是 address
。 facet_wrap()
的参数 scales = "free_x"
确保未使用的级别将从构面中删除。但是,需要通过向 scale_x_discrete()
的 labels
参数提供命名向量来替换 x 轴上的标签。
library(ggplot2) # version 2.2.1 used
ggplot(df_ex, aes(x=ordered,y="",fill=clas)) + #x axis bias voltage dependence
geom_tile() +
scale_fill_manual(values=c('Good'="green","Bad"="Blue","Ugly"="black"))+
facet_wrap(~No,ncol=1,scales = "free_x")+
theme(legend.position = "top",axis.text.y = element_text(size = 20,angle = 90),axis.text.x = element_text(size=12,face="bold",colour = "black"),
axis.title.y = element_text(face="bold",size = 20, colour = "black"),
axis.title.x = element_text(face="bold",size = 20 , colour = "black"),
strip.text = element_text(size=26, face="bold"),
strip.background = element_rect(fill="#FFFF66", colour="black", size=0.5),
plot.title=element_text(face="bold",color="red",size=14),
legend.title = element_text(colour="black", size=26,face="bold"),
legend.text = element_text(colour="black", size=18))+
labs(x = "address",y = "") +
scale_x_discrete(labels = setNames(df_ex$address, df_ex$ord)) +
关于r - facet_wrap 中因子级别的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41199496/
我有这种格式的data.frame: 'data.frame': 244 obs. of 1 variable: $ names: Factor w/ 244 levels "ERA","BA
这就是问题: write a Java Program that accepts a String and an integer stretch factor P as parameters and
该示例显示了不同工厂的产量测量值,第一列表示工厂最后一列是生产量。 factory % mutate(factory=fct_lump(factory,2)) factory produc
我正在使用分类变量运行回归并遇到 this question .在这里,用户想要为每个虚拟对象添加一列。这让我很困惑,因为我虽然列有很长的数据,包括使用 as.factor() 存储的所有虚拟数据。相
假设在 R 中有一个 Data.Frame 对象,其中所有字符列都已转换为因子。然后我需要“修改”与数据帧中某一行相关联的值——但将其编码为一个因子。我首先需要提取一行,所以这就是我正在做的。这是一个
利用下面的可重现数据, dat head(dat) Bin Number 1 1 3 2 1 5 3 1 4 4 1 5 5 1
我有一组包含多个变量的数据。其中一个变量 - 阶乘包含组的名称 - A、B、C 等。其余变量是数字。 > data1 Group Value 1 A 23 2 A
我有一组编码为二项式的变量。 Pre VALUE_1 VALUE_2 VALUE_3 VALUE_4 VALUE_5 VALUE_6 VALUE_7 VALUE_8 1 1 0
我的问题与 this one 非常相似和 this other one ,但我的数据集有点不同,我似乎无法使这些解决方案起作用。如果我误解了什么并且这个问题是多余的,请原谅。 我有一个这样的数据集:
我一直在尝试生成一个带有离散 x 变量的堆积面积图(因为我想显示财政年度,即“2013/14”,而不是日历年)。但是,将 x 轴变量转换为一个因子会阻止在最终图表中呈现 geom。 有解决办法吗? l
只是一个简单的问题来确认我的想法, 使用负载因子 1.0 的哈希表的复杂性将是二次时间,用以下符号 O(n^2) 表示。 这是因为必须不断调整大小并一遍又一遍地插入。如果我错了,请纠正我。 谢谢 最佳
我正在尝试使用 kaggle 的一些数据集进行房价预测。 这是我的代码 library(ggplot2) dataset=read.csv('train(1).csv') dataset_test=r
我正在用 Angular 构建一个类似咆哮的 UI。我想将其公开为工厂(或服务),以使其在我的 Controller 中可用。调用 Growl.add 将导致 DOM 发生变化,所以看起来我应该有一个
我正在尝试将 pandas 数据框的一列转换为因数,因为我试图在 R 中调用的函数需要因数。 pandas2ri.activate() #second column of labels has
我正在尝试使用 plotly 绘制一个以字符串(组合数)作为 x 轴的条形图。 (“1”、“2”、“3”、“4 - 5”、“6 - 8”、“9 - 13”、“14 - 21”、“22 - 34”、“3
我有一个包含 NA 的数据集。 此外,它还有一些列需要factors()。 我正在使用 caret 包中的 rfe() 函数来选择变量。 似乎 rfe() 中的 functions= 参数使用 lmF
我有一个 .csv 文件,其中每个字段用于日期时间、日期和时间。 最初它们都是字符字段,我已经相应地转换了它们。 在我的代码结束时,如果我这样做: str(data) 我会得到 datetime: P
我有一个如下所示的数据集: data.flu data.flu chills runnyNose headache fever flu 1 1 0 M
我正在使用 QMainWindow 在 C++ 中手动布置 Qt 应用程序。我希望在屏幕底部有两个并排停靠的小部件,但我希望它们具有不成比例的宽度。目前,我只能让它们具有相同的宽度。有没有办法设置拉伸
我需要通过在两个主机(2 个 Java 进程)之间发送合成调用来计算 VOIP 质量。我应该找出 MOS、抖动和 R 因子(VOIP 质量指标)。根据目前的研究,我发现我应该在两台主机之间发送 RTP
我是一名优秀的程序员,十分优秀!