我正在从事儿童性虐待研究。为此,我想使用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
变量的顺序排序:&#34;制作性爱视频&#34;,&#34;强迫性行为&#34;,&#34;看着在私人部分&#34;,&#34;触摸私人部分&#34;,&#34;显示色情&#34; &#34;以性方式谈话&#34;。
在每个群集中,图表必须按Familiarity_with_the_perpetrator
变量的顺序排序:&#34;非常熟悉&#34;,&#34;不太熟悉&#34;和#34;根本不熟悉&#34;。
我尝试输入数据,以便我想要显示它。但是,lattice
似乎会按字母顺序自动对组进行排序。
我还希望每个栏的值显示在栏的顶部。有人可以帮我按照我想要的顺序对它进行分组吗?
正如你所看到的,我是R的新手。所以,任何帮助都会受到很多赞赏。
答案 0 :(得分:1)
您的列 Nature_of_sexual_abuse 和 Familiarity_with_the_perpetrator 是分类变量的因素。类型因子的数据始终具有R中的级别,这是您数据中存在的唯一字符值。绘制数据时,这些级别值将用作标签。
例如,在您的数据中,列 Nature_of_sexual_abuse 的级别具有唯一值“制作性爱视频”,“强迫性行为”,“看私处”,“触摸私处” ,“显示色情”和“以性方式谈话”。
如果要在图中重新排序数据,则需要重新排序分类数据的级别值。 在创建绘图之前插入此重新排序,它应该可以工作:
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))
)
根据您的关卡顺序,图表中数据的顺序会发生变化。
我希望这可以帮助您解决订购数据的问题。