lattice: Clustered Barchart: 排序条形并在条形顶部显示值
lattice: Clustered Barchart: Sort bars and display values at top of bars
我正在从事一项儿童性虐待研究。为此,我想使用 lattice
准备一个聚类条形图。我使用了以下代码。
我的数据:
nature <- read.table(text = "Nature_of_sexual_abuse, Familiarity_with_the_perpetrator, Count
Talked in a sexual way, Not familiar at all, 21
Talked in a sexual way, Not very familiar, 22
Talked in a sexual way, Very familiar, 22
Shown pornography, Not familiar at all, 6
Shown pornography, Not very familiar, 17
Shown pornography, Very familiar, 16
Looked at private parts, Not familiar at all, 16
Looked at private parts, Not very familiar, 14
Looked at private parts, Very familiar, 10
Touched private parts, Not familiar at all, 6
Touched private parts, Not very familiar, 15
Touched private parts, Very familiar, 11
Made a sex video, Not familiar at all, 1
Made a sex video, Not very familiar, 6
Made a sex video, Very familiar, 7
Forced sex behaviors, Not familiar at all, 5
Forced sex behaviors, Not very familiar, 17
Forced sex behaviors, Very familiar, 10",
header = TRUE,
sep = ",", strip.white = TRUE)
我的剧情:
library(lattice)
colors = c("lightsalmon3", "lightgoldenrod2", "cadetblue4")
barchart(
data = nature,
origin = 0,
Count ~ Nature_of_sexual_abuse,
groups = Familiarity_with_the_perpetrator,
xlab = list (
label = "Nature of sexual abuse",
font = 2,
cex = 1),
ylab= list (
label = "Number of students",
font = 2,
cex = 1),
ylim=c(0,25),
labels = TRUE,
auto.key = list(space="top", columns= 3),
par.settings = list(superpose.polygon = list(col = colors)))
图表目前看起来像这样:
但是,我希望它按照 Nature_of_sexual_abuse
变量的以下顺序排序:"Made a sex video"、"Forced sex behaviors"、"Looked at private parts"、"Touched private parts"、"Shown pornography" 和 "Talked in a sexual way".
在每个集群中,图表必须按照 Familiarity_with_the_perpetrator
变量的顺序排序:"Very familiar"、"Not very familiar" 和 "Not familiar at all"。
我尝试按要显示的顺序输入数据。但是,lattice
似乎会自动按字母顺序对组进行排序。
我还希望每个条形图的值显示在条形图的顶部。有人可以帮我按我想要的顺序分组吗?
如您所见,我是 R 的新手。因此,我们将不胜感激任何帮助。
您的列 Nature_of_sexual_abuse 和 Familiarity_with_the_perpetrator 是属于分类变量的因子。因子类型的数据在 R 中始终具有级别,这些级别是数据中存在的唯一字符值。当您绘制数据时,这些水平值用作标签。
例如,在您的数据中,列 Nature_of_sexual_abuse 的水平具有唯一值 "Made a sex video"、"Forced sex behaviors"、"Looked at private parts"、"Touched private parts"、"Shown pornography" 和 "Talked in a sexual way"。
如果您想重新排序图中的数据,您需要重新排序分类数据的水平值。
在创建绘图之前插入此重新排序,它应该可以工作:
library(lattice)
nature <- read.table(text = "Nature_of_sexual_abuse,
Familiarity_with_the_perpetrator, Count
Talked in a sexual way, Not familiar at all, 21
Talked in a sexual way, Not very familiar, 22
Talked in a sexual way, Very familiar, 22
Shown pornography, Not familiar at all, 6
Shown pornography, Not very familiar, 17
Shown pornography, Very familiar, 16
Looked at private parts, Not familiar at all, 16
Looked at private parts, Not very familiar, 14
Looked at private parts, Very familiar, 10
Touched private parts, Not familiar at all, 6
Touched private parts, Not very familiar, 15
Touched private parts, Very familiar, 11
Made a sex video, Not familiar at all, 1
Made a sex video, Not very familiar, 6
Made a sex video, Very familiar, 7
Forced sex behaviors, Not familiar at all, 5
Forced sex behaviors, Not very familiar, 17
Forced sex behaviors, Very familiar, 10",
header = TRUE,
sep = ",", strip.white = TRUE)
nature$Nature_of_sexual_abuse <- factor(nature$Nature_of_sexual_abuse,
levels=c("Made a sex video",
"Forced sex behaviors",
"Looked at private parts",
"Touched private parts",
"Shown pornography",
"Talked in a sexual way"))
nature$Familiarity_with_the_perpetrator <-
factor(nature$Familiarity_with_the_perpetrator, levels=c("Very familiar",
"Not very familiar","Not familiar at all"))
colors = c("lightsalmon3", "lightgoldenrod2", "cadetblue4")
barchart( data = nature,
origin = 0,
Count ~ Nature_of_sexual_abuse,
groups = Familiarity_with_the_perpetrator,
xlab = list (label = "Nature of sexual abuse",
font = 2, cex = 1),
ylab= list ( label = "Number of students",
font = 2, cex = 1),
ylim=c(0,25),
labels = TRUE,
auto.key = list(space="top", columns= 3),
par.settings = list(superpose.polygon = list(col = colors))
)
根据您的水平顺序,图中数据的顺序会发生变化。
希望这对您订购数据的问题有所帮助。
我正在从事一项儿童性虐待研究。为此,我想使用 lattice
准备一个聚类条形图。我使用了以下代码。
我的数据:
nature <- read.table(text = "Nature_of_sexual_abuse, Familiarity_with_the_perpetrator, Count
Talked in a sexual way, Not familiar at all, 21
Talked in a sexual way, Not very familiar, 22
Talked in a sexual way, Very familiar, 22
Shown pornography, Not familiar at all, 6
Shown pornography, Not very familiar, 17
Shown pornography, Very familiar, 16
Looked at private parts, Not familiar at all, 16
Looked at private parts, Not very familiar, 14
Looked at private parts, Very familiar, 10
Touched private parts, Not familiar at all, 6
Touched private parts, Not very familiar, 15
Touched private parts, Very familiar, 11
Made a sex video, Not familiar at all, 1
Made a sex video, Not very familiar, 6
Made a sex video, Very familiar, 7
Forced sex behaviors, Not familiar at all, 5
Forced sex behaviors, Not very familiar, 17
Forced sex behaviors, Very familiar, 10",
header = TRUE,
sep = ",", strip.white = TRUE)
我的剧情:
library(lattice)
colors = c("lightsalmon3", "lightgoldenrod2", "cadetblue4")
barchart(
data = nature,
origin = 0,
Count ~ Nature_of_sexual_abuse,
groups = Familiarity_with_the_perpetrator,
xlab = list (
label = "Nature of sexual abuse",
font = 2,
cex = 1),
ylab= list (
label = "Number of students",
font = 2,
cex = 1),
ylim=c(0,25),
labels = TRUE,
auto.key = list(space="top", columns= 3),
par.settings = list(superpose.polygon = list(col = colors)))
图表目前看起来像这样:
但是,我希望它按照 Nature_of_sexual_abuse
变量的以下顺序排序:"Made a sex video"、"Forced sex behaviors"、"Looked at private parts"、"Touched private parts"、"Shown pornography" 和 "Talked in a sexual way".
在每个集群中,图表必须按照 Familiarity_with_the_perpetrator
变量的顺序排序:"Very familiar"、"Not very familiar" 和 "Not familiar at all"。
我尝试按要显示的顺序输入数据。但是,lattice
似乎会自动按字母顺序对组进行排序。
我还希望每个条形图的值显示在条形图的顶部。有人可以帮我按我想要的顺序分组吗?
如您所见,我是 R 的新手。因此,我们将不胜感激任何帮助。
您的列 Nature_of_sexual_abuse 和 Familiarity_with_the_perpetrator 是属于分类变量的因子。因子类型的数据在 R 中始终具有级别,这些级别是数据中存在的唯一字符值。当您绘制数据时,这些水平值用作标签。
例如,在您的数据中,列 Nature_of_sexual_abuse 的水平具有唯一值 "Made a sex video"、"Forced sex behaviors"、"Looked at private parts"、"Touched private parts"、"Shown pornography" 和 "Talked in a sexual way"。
如果您想重新排序图中的数据,您需要重新排序分类数据的水平值。 在创建绘图之前插入此重新排序,它应该可以工作:
library(lattice)
nature <- read.table(text = "Nature_of_sexual_abuse,
Familiarity_with_the_perpetrator, Count
Talked in a sexual way, Not familiar at all, 21
Talked in a sexual way, Not very familiar, 22
Talked in a sexual way, Very familiar, 22
Shown pornography, Not familiar at all, 6
Shown pornography, Not very familiar, 17
Shown pornography, Very familiar, 16
Looked at private parts, Not familiar at all, 16
Looked at private parts, Not very familiar, 14
Looked at private parts, Very familiar, 10
Touched private parts, Not familiar at all, 6
Touched private parts, Not very familiar, 15
Touched private parts, Very familiar, 11
Made a sex video, Not familiar at all, 1
Made a sex video, Not very familiar, 6
Made a sex video, Very familiar, 7
Forced sex behaviors, Not familiar at all, 5
Forced sex behaviors, Not very familiar, 17
Forced sex behaviors, Very familiar, 10",
header = TRUE,
sep = ",", strip.white = TRUE)
nature$Nature_of_sexual_abuse <- factor(nature$Nature_of_sexual_abuse,
levels=c("Made a sex video",
"Forced sex behaviors",
"Looked at private parts",
"Touched private parts",
"Shown pornography",
"Talked in a sexual way"))
nature$Familiarity_with_the_perpetrator <-
factor(nature$Familiarity_with_the_perpetrator, levels=c("Very familiar",
"Not very familiar","Not familiar at all"))
colors = c("lightsalmon3", "lightgoldenrod2", "cadetblue4")
barchart( data = nature,
origin = 0,
Count ~ Nature_of_sexual_abuse,
groups = Familiarity_with_the_perpetrator,
xlab = list (label = "Nature of sexual abuse",
font = 2, cex = 1),
ylab= list ( label = "Number of students",
font = 2, cex = 1),
ylim=c(0,25),
labels = TRUE,
auto.key = list(space="top", columns= 3),
par.settings = list(superpose.polygon = list(col = colors))
)
根据您的水平顺序,图中数据的顺序会发生变化。
希望这对您订购数据的问题有所帮助。