如何让ggplot按照数据的先后顺序通达信画线数据

用ggplot2在一张图上同时画多个变量的曲线
转自:/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph
very newbish question, but say I have data like this:
test_data &- data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
date = seq.Date(as.Date(""), by="1 month", length.out=100))
How can I plot both time series&var0&and&var1&on
the same graph, with&date&on
the x-axis, using&ggplot2?
Bonus points if you make&var0&and&var1&different
colours, and can include a legend!
general approach is to convert the data to long format
(using&melt()&from
package&reshape&or&reshape2)
require("reshape")
require("ggplot2")
test_data_long &- melt(test_data, id="date")
# convert to long format
ggplot(data=test_data_long,
aes(x=date, y=value, colour=variable)) +
geom_line()
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。是什么使得使用ggplot一个情节,当hjust和vjust吗?
我每次使用ggplot做一个情节,我花了一小会儿像一条线尝试了hjust和vjust不同的价值观
+ opts(axis.text.x = theme_text(hjust = 0.5))
拿到轴标签排队,其中轴标签几乎触及轴,并且反对平齐(调整到轴,可以这么说)。 不过,我真的不明白是怎么回事。 通常情况下, hjust = 0.5给出了这样的显着不同的结果hjust = 0.6 ,例如,我一直无法仅仅通过使用不同的值玩弄弄明白。
任何人都可以点我的选项是如何hjust和vjust工作进行全面的解释吗?
--------------解决方案-------------
的值hjust和vjust仅在0和1之间限定:
左对齐0手段
1表示右对齐
来源:GGPLOT2,哈德利韦翰,第196页
(是的,我知道,在大多数情况下,你可以用它超出此范围,但不要指望它在任何特定的方式行事,这是外面的规范。)
hjust控制水平对齐和vjust控制垂直对齐。
一个例子应该明确这一点:
td &- expand.grid(
hjust=c(0, 0.5, 1),
vjust=c(0, 0.5, 1),
angle=c(0, 45, 90),
text=&text&
ggplot(td, aes(x=hjust, y=vjust)) +
geom_point() +
geom_text(aes(label=text, angle=angle, hjust=hjust, vjust=vjust)) +
facet_grid(~angle) +
scale_x_continuous(breaks=c(0, 0.5, 1), expand=c(0, 0.2)) +
scale_y_continuous(breaks=c(0, 0.5, 1), expand=c(0, 0.2))
要了解,当你改变了会发生什么hjust在轴的文字,你需要明白,轴文本的水平对齐方式的关系不是x轴被定义,而是对整个剧情(如果这包括y轴文本) 。
(这一点,在我看来,不幸的,这将是有用得多有相对轴线对齐。)
DF &- data.frame(x=LETTERS[1:3],y=1:3)
p &- ggplot(DF, aes(x,y)) + geom_point() +
ylab(&Very long label for y&) +
opts(axis.title.y=theme_text(angle=0))
p1 &- p + opts(axis.title.x=theme_text(hjust=0)) + xlab(&X-axis at hjust=0&)
p2 &- p + opts(axis.title.x=theme_text(hjust=0.5)) + xlab(&X-axis at hjust=0.5&)
p3 &- p + opts(axis.title.x=theme_text(hjust=1)) + xlab(&X-axis at hjust=1&)
library(ggExtra)
align.plots(p1, p2, p3)
为了探讨与个究竟vjust轴标签的aligment:
DF &- data.frame(x=c(&a\na&,&b&,&cdefghijk&,&l&),y=1:4)
p &- ggplot(DF, aes(x,y)) + geom_point()
p1 &- p + opts(axis.text.x=theme_text(vjust=0, colour=&red&)) +
xlab(&X-axis labels aligned with vjust=0&)
p2 &- p + opts(axis.text.x=theme_text(vjust=0.5, colour=&red&)) +
xlab(&X-axis labels aligned with vjust=0.5&)
p3 &- p + opts(axis.text.x=theme_text(vjust=1, colour=&red&)) +
xlab(&X-axis labels aligned with vjust=1&)
library(ggExtra)
align.plots(p1, p2, p3)
也许最权威的是书GGPLOT2图B.1(D),附录,其中可在http://had.co.nz/ggplot2/book/appendices.pdf。
然而,这是不是很简单。 hjust和vjust的描述有它的工作原理在geom_text和theme_text (有时)。 想起来的一种方法是考虑周围的文本框的,并且其中所述参考点是相对于该盒,相对于盒的尺寸(从而为不同尺寸的文本不同)单元。 一个hjust 0.5和vjust 0.5中心上的基准点的框。 减少hjust权由框宽度倍的量移动框0.5-hjust 。 因此,当hjust=0 ,盒子的左边缘处的基准点。 增加hjust移动的框宽度倍的量离开了包厢hjust-0.5 。 当hjust=1 ,盒被移动从中心向左半盒宽度,这使右边缘上的参考点。 如果hjust=2 ,框的右边缘离开所述参考点的框的宽度(中心是2-0.5=1.5左参考点的框的宽度。对于垂直,以下是向上和更是向下,这实际上是什么图B.1(D)说,但它超越推断[0,1]。
不过,有时候这是行不通的。 例如
DF &- data.frame(x=c(&a&,&b&,&cdefghijk&,&l&),y=1:4)
p &- ggplot(DF, aes(x,y)) + geom_point()
p + opts(axis.text.x=theme_text(vjust=0))
p + opts(axis.text.x=theme_text(vjust=1))
p + opts(axis.text.x=theme_text(vjust=2))
后三种曲线是相同的。 我不知道这是为什么。 此外,如果文本被旋转,那么它是比较复杂的。 考虑
p + opts(axis.text.x=theme_text(hjust=0, angle=90))
p + opts(axis.text.x=theme_text(hjust=0.5 angle=90))
p + opts(axis.text.x=theme_text(hjust=1, angle=90))
p + opts(axis.text.x=theme_text(hjust=2, angle=90))
第一个有标签左对齐(对底部),第二个有他们在一些对话框,以便它们的中心排队,第三个有他们说得有道理中心(所以他们的左右两侧排队旁轴)。 最后一个,好了,我无法解释一个连贯的方式。 它是与文字,最宽的文本的大小的尺寸,我不知道还有什么。
Copyright (C) , All Rights Reserved.
版权所有 京ICP备号
processed in 0.707 (s). 11 q(s)苹果/安卓/wp
积分 302, 距离下一级还需 148 积分
权限: 自定义头衔, 签名中使用图片
道具: 彩虹炫, 涂鸦板, 雷达卡, 热点灯, 金钱卡, 显身卡, 匿名卡下一级可获得
道具: 抢沙发
购买后可立即获得
权限: 隐身
道具: 金钱卡, 彩虹炫, 雷达卡, 热点灯, 涂鸦板
本帖最后由 xlyshuai 于
10:21 编辑
请问在使用ggplot2画图时,能不能控制X轴的排序?
如下例:以from_ent_level& &为X轴,以code_count& &为y轴画图,画出的样子如下图,X轴排序是(1,2,3,4,生产出库,终端入库)。我希望的排序是以orderID列为序号显示,即X轴的排序为(生产出库,1,2,3,4,终端入库),请问有什么办法实现?
十分感谢!
以下是程序、生成图、数据:
lab_Y& && && && && &&&&&- paste(&出库量(万&,vDrugPkgSpec,&)\n&,sep = &&)& && &##& && &&&y轴标题
p1 &- ggplot(data = df_report_1_toPPT2, aes(x=from_ent_level,y=df_report_1_toPPT2$code_count))& &&&## 图形数据
p1 &- p1 + geom_bar( stat=&identity& , width = 0.5, fill = &cornflowerblue&)& &##&&图层,柱状大小.颜色
p1 &- p1 + geom_text(label=label_Y,colour = &blue&, vjust=-1)& && &&&## 柱状上侧数字.颜色
p1 &- p1 + labs(x=&\n生产、各流通级别、终端&,y=lab_Y,title = &生产、各流通级别、终端流通量和纯销比\n&) ## 图形标题.x/y轴标题
p1 &- p1 + theme(axis.text.x =&&element_text(angle = 00, hjust = 0.5, size=12*1.33, color=&black&))&&## x坐标轴文字大小.方向.颜色
p1 &- p1 + theme(axis.text.y =&&element_text(angle = 00, hjust = 1, size=12*1.33, color=&black&))& &## y坐标轴文字大小.方向.颜色
p1 &- p1 + theme(axis.title.x = element_text(size = 12*1.33, angle = 00)) ## x坐标轴标题大小.方向.颜色
p1 &- p1 + theme(axis.title.y = element_text(size = 12*1.33, angle = 90)) ##& &y坐标轴标题大小.方向.颜色
p1 &- p1 + scale_y_continuous(limits=c(0, max(aex_Y)*1.1),labels = comma) # 调整y坐标显示方式
p1 &- p1 + theme( plot.title = element_text(size = 16*1.33, face = &bold&))
10:10:24 上传
&&orderID&&from_ent_level& &code_count&&
& && & 1& && & 生产出库& && && && & 325920& && &
& && & 3& && && && &&&1& && && && && && &20280& && && &
& && & 4& && && && &&&2& && && && && && &50760& && && &&&
& && & 5& && && && &&&3& && && && && && &7813& && && &&&
& && & 6& && && && &&&4& && && && && &&&620& && && && && &
& && & 9& && & 终端购进& && && && &13292& && &&&
方法1方法2
支持楼主:、
购买后,论坛将把您花费的资金全部奖励给楼主,以表示您对TA发好贴的支持
载入中......
本帖最后由 dataorz 于
14:10 编辑
df_report_1_toPPT2$orderID &- as.integer(rownames(df_report_1_toPPT2))
p1 &- ggplot(data = df_report_1_toPPT2, aes(x=orderID,y=df_report_1_toPPT2$code_count))
p1 &- p1 + geom_bar( stat=&identity& , width = 0.5, fill = &cornflowerblue&)
p1 + scale_x_continuous(breaks=df_report_1_toPPT2$orderID, labels=df_report_1_toPPT2$from_ent_level)复制代码方法1 df_report_1_toPPT2$from_ent_level &- factor(as.integer(rownames(df_report_1_toPPT2)),labels=df_report_1_toPPT2$from_ent_level)
p1 &- ggplot(data = df_report_1_toPPT2, aes(x=from_ent_level,y=df_report_1_toPPT2$code_count))
p1 + geom_bar( stat=&identity& , width = 0.5, fill = &cornflowerblue&)复制代码方法2
dataorz 发表于
方法1方法2十分感谢!两种方法都很棒!
&nbsp&nbsp|
&nbsp&nbsp|
&nbsp&nbsp|
&nbsp&nbsp|
&nbsp&nbsp|
&nbsp&nbsp|
如有投资本站或合作意向,请联系(010-);
邮箱:service@pinggu.org
投诉或不良信息处理:(010-)
论坛法律顾问:王进律师I have created a bar graph in ggplot2 where 3 bars represent the probability of making 1 of 3 choices.
I want to add a bolded border around the bar that shows the correct response.
I haven't found a way to do this. I can change the colour of ALL the bars but not just the one.
The image attached shows the grid of graphs I have generated. In the leftCust column I want all bars with 'left' below them to have a bold border.
In the rightCust column I want to add the bold border to all bars with right below them.
And finally in the SIMCust column I want all bars with SIM below them to have a bold border.
This is basically to highlight the correct response and make it easier to explain what the graphs are showing.
Any ideas?
Thanks for your time.
dataRarrangeExpD &- read.csv("C:/Documents and Settings/psundere/My Documents/Analysis/Exp2D/EXP2D.csv", header =TRUE);
# NEXT Calculate means or just build Graphs?
library(ggplot2)
library("matrixStats")
library("lattice")
library("gdata")
library(plyr)
library(doBy)
library(Epi)
library(reshape2)
library(graphics)
#Create DataFrame with only Left-to-Right Visual Presentation
DataRearrangeD &- dataRarrangeExpD[, c("correct","Circle1", "Beep1","correct_response", "response", "subject_nr")]
#data_exp1$target_coh & 0
# ,DataRearrange$response, DataRearrange$correct_response
"Response", "Accuracy"
# Add new columns to hold choices made
DataRearrangeD[c("RightChoice", "LeftChoice", "SimChoice")] &- 0
DataRearrangeD$RightChoice &- ifelse(DataRearrangeD$response == "l", 1, 0)
DataRearrangeD$LeftChoice &- ifelse(DataRearrangeD$response == "a", 1, 0)
DataRearrangeD$SimChoice &- ifelse(DataRearrangeD$response == "space", 1, 0)
Exp2D.data = DataRearrangeD
# Construct data frames of report probability
SIM.vis.aud.df = aggregate(SimChoice ~ Circle1 + Beep1 + subject_nr, data = Exp2D.data, mean)
RightFirst.vis.aud.df = aggregate(RightChoice ~ Circle1 + Beep1 + subject_nr, data = Exp2D.data, mean)
LeftFirst.vis.aud.df = aggregate(LeftChoice ~ Circle1 + Beep1 + subject_nr, data = Exp2D.data, mean)
# combine data frames
mean.vis.aud.df = data.frame(SIM.vis.aud.df, RightFirst.vis.aud.df$RightChoice, LeftFirst.vis.aud.df$LeftChoice)
colnames(mean.vis.aud.df)[5:5] = c("Right")
colnames(mean.vis.aud.df)[6:6] = c("Left")
colnames(mean.vis.aud.df)[4:4] = c("SIM")
colnames(mean.vis.aud.df)[1:2] = c("Visual", "Audio")
# using reshape 2, we change the data frame to long format## measure.var column 3 up to column 5 i.e. 3,4,5
mean.vis.aud.long = melt(mean.vis.aud.df, measure.vars = 4:6, variable.name = "Report", value.name = "Prob")
# re-order levels of Report for presentation purposes
mean.vis.aud.long$Report = Relevel(mean.vis.aud.long$Report, ref = c("Left", "SIM", "Right"))
mean.vis.aud.long$Visual = Relevel(mean.vis.aud.long$Visual, ref = c("LeftCust","SIMCust","RightCust"))
#write.table(mean.vis.aud.long, "C:/Documents and Settings/psundere/My Documents/Analysis/Exp2_Pilot/reshape.txt",row.names=F)
##############################################################################################
##############################################################################################
# Calculate SD, SE Means etc.
##############################################################################################
##############################################################################################
CalSD &- mean.vis.aud.long[, c("Prob", "Report", "Visual", "Audio", "subject_nr")]
# Get the average effect size by Prob
CalSD.means &- aggregate(CalSD[c("Prob")],
by = CalSD[c("subject_nr", "Report", "Visual", "Audio")], FUN=mean)
#"correct","Circle1", "Beep1","correct_response", "response", "subject_nr"
# multiply by 100
CalSD.means$Prob &- CalSD.means$Prob*100
# Get the sample (n-1) standard deviation for "Probability"
CalSD.sd &- aggregate(CalSD.means["Prob"],
by = CalSD.means[c("Report","Visual", "Audio")], FUN=sd)
# Calculate SE --& SD / sqrt(N)
CalSD.se &- CalSD.sd$Prob / sqrt(25)
SE &- CalSD.se
# Confidence Interval @ 95% --& Standard Error * qt(0.975, N-1) SEE help(qt)
#.975 instead of .95 becasuse the 5% is 2.5% either side of the distribution
ci &- SE*qt(0.975,24)
##############################################################################################
##############################################################################################
###################################################
# Bar Graph
#mean.vis.aud.long$Audio &- factor (mean.vis.aud.long$Audio, levels = c("left", "2centre","NoBeep", "single","right"))
AggBar &- aggregate(mean.vis.aud.long$Prob*100,
by=list(mean.vis.aud.long$Report,mean.vis.aud.long$Visual, mean.vis.aud.long$Audio),FUN="mean")
#Change column names
colnames(AggBar) &- c("Report", "Visual", "Audio","Prob")
# Change the order of presentation
#CondPerRow$AuditoryCondition &- factor (CondPerRow$AuditoryCondition, levels = c("NoBeep", "left", "right"))
prob.bar = ggplot(AggBar, aes(x = Report, y = Prob, fill = Report)) + theme_bw() + facet_grid(Audio~Visual)
prob.bar + geom_bar(position=position_dodge(.9), stat="identity", colour="black") + theme(legend.position = "none") + labs(x="Report", y="Probability of Report") + scale_fill_grey() +
labs(title = expression("Visual Condition")) +
theme(plot.title = element_text(size = rel(1)))+
geom_errorbar(aes(ymin=Prob-ci, ymax=Prob+ci),
width=.2, # Width of the error bars
position=position_dodge(.9))+
theme(plot.title = element_text(size = rel(1.5)))+
scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 10)))
This is what AggBar looks like after manipulation just before generating the graph:
2centre 81.84
SIM LeftCust
2centre 13.52
2centre 4.64
SIMCust 2centre 17.36
SIM SIMCust 2centre 69.76
SIMCust 2centre 12.88
2centre 8.88
SIM RightCust
2centre 13.12
2centre 78.00
SIM LeftCust
SIMCust left
SIM SIMCust left
SIMCust left
SIM RightCust
SIM LeftCust
SIMCust NoBeep
SIM SIMCust NoBeep
SIMCust NoBeep
SIM RightCust
SIM LeftCust
SIMCust right
SIM SIMCust right
SIMCust right
SIM RightCust
SIM LeftCust
SIMCust single
SIM SIMCust single
SIMCust single
SIM RightCust
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Using the code put forward by Troy below I put a little twist on it and came up with a wee solution to the lack of patterns in ggplot2 for bar graphs.
Here's the code I used to add vertical lines to the bars to achieve a basic pattern for the correct response bars. I'm sure you clever folk out there could adapt this for your own needs with regard texture/patterns albeit basic ones:
######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
HighlightDataCust &-AggBar[AggBar$Report==gsub("Cust", "", AggBar$Visual),]
#####################################################
prob.bar = ggplot(AggBar, aes(x = Report, y = Prob, fill = Report)) + theme_bw() + facet_grid(Audio~Visual)
prob.bar + geom_bar(position=position_dodge(.9), stat="identity", colour="black") + theme(legend.position = "none") + labs(x="Response", y="Probability of Report") + scale_fill_grey() +
######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
geom_bar(data=HighlightDataCust, position=position_dodge(.9), stat="identity", colour="black", size=2)+
geom_bar(data=HighlightDataCust, position=position_dodge(.9), stat="identity", colour="black", size=0.5, width=0.85)+
geom_bar(data=HighlightDataCust, position=position_dodge(.9), stat="identity", colour="black", size=0.5, width=0.65)+
geom_bar(data=HighlightDataCust, position=position_dodge(.9), stat="identity", colour="black", size=0.5, width=0.45)+
geom_bar(data=HighlightDataCust, position=position_dodge(.9), stat="identity", colour="black", size=0.5, width=0.25)+
geom_bar(data=HighlightDataCust, position=position_dodge(.9), stat="identity", colour="black", width=0.0) +
######################################################
labs(title = expression("Visual Condition")) +
theme(text=element_text(size=18))+
theme(axis.title.x=element_text(size=18))+
theme(axis.title.y=element_text(size=18))+
theme(axis.text.x=element_text(size=12))+
geom_errorbar(aes(ymin=Prob-ci, ymax=Prob+ci),
width=.2, # Width of the error bars
position=position_dodge(.9))+
theme(plot.title = element_text(size = 18))+
scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 10)))
This is the output. Clearly the lines can be made any colour you wish and a mix of colours. Just make sure you start off with the widest width and and work towards 0.0 so the layers don't over-write. Hope someone finds this useful. (It should also be possible to create horizontal lines inside bars if one were to create multiple layers with different y-axis heights i.e. the top of each differing bar height would appear like a horizontal line. Haven't tested this myself but it may be worth looking into for those that require more than one bar pattern. Combining both in one bar should result in a mesh pattern and forget not that different colours can also be used. In short I think this approach is a decent fix for the lack of pattern in ggplot2.)
I have created an example of the 3 types of pattern I mentioned here:
解决方案 I haven't got your data so I have used the diamonds dataset to demonstrate.
Basically you need to 'overplot' a second geom_bar() call, where you filter the data= attribute to only draw the bars you want to highlight. Just filter the original data to exclude anything you don't want. e.g below we replot the subset diamonds[(diamonds$clarity=="SI2"),]
d &- ggplot(diamonds) +
geom_bar(aes(clarity, fill=color))
# first plot
d + geom_bar(data=diamonds[(diamonds$clarity=="SI2"),],
aes(clarity), alpha=0, size=1, color="black") +
# plot outline only
facet_wrap(~ cut)
NB obviously your filter will be more complicated, e.g.
data=yourdata[(yourdata$visualcondition=="LeftCust" & yourdata$report=="Left" |
yourdata$visualcondition=="SIMCust" & yourdata$report=="SIM" |
yourdata$visualcondition=="RightCust" & yourdata$report=="Right"),]
OK updated with your data. I had to make up confidence intervals because they weren't available in the AggBar2 data:
######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
HighlightData&-AggBar2[AggBar2$Report==gsub("Cust","",AggBar2$Visual),]
#####################################################
prob.bar = ggplot(AggBar2, aes(x = Report, y = Prob, fill = Report)) + theme_bw() + facet_grid(Audio~Visual)
prob.bar + geom_bar(position=position_dodge(.9), stat="identity", colour="black") + theme(legend.position = "none") + labs(x="Report", y="Probability of Report") + scale_fill_grey() +
######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
geom_bar(data=HighlightData, position=position_dodge(.9), stat="identity", colour="pink",size=1) +
######################################################
labs(title = expression("Visual Condition")) +
theme(plot.title = element_text(size = rel(1)))+
geom_errorbar(aes(ymin=Prob-ci, ymax=Prob+ci),
width=.2, # Width of the error bars
position=position_dodge(.9))+
theme(plot.title = element_text(size = rel(1.5)))+
scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 10)))
本文地址: &
我在ggplot2中创建了一个条形图,其中3条表示选择3个选项中的1个的概率。
我要添加 bolded 边框,显示正确的回应。
我没有找到办法做到这一点。我可以更改所有条形的颜色,但不能只改变一个。
附图显示了我生成的图形的网格。在leftCust列中,我希望所有在其下面都有“left”的栏都有一个粗体边框。
在rightCust列中,我要将粗体边框添加到所有栏
最后,在SIMCust列中,我希望所有带有SIM卡的条形都有一个粗体边框。
这基本上是为了突出正确的响应,并使它更容易解释图表显示。
有任何想法吗?
感谢您的时间。
dataRarrangeExpD&
read.csv(“C:/ Documents and Settings / psundere / My Documents / Analysis / Exp2D / EXP2D .csv“,header = TRUE);
#NEXT计算平均值或只是构建图表? 库(ggplot2)库(“matrixStats”)库(“lattice”)库(“gdata”)库(plyr)库(doBy)库(Epi)库(reshape2)库(图形)
#仅创建从左到右的DataFrame视觉呈现 DataRearrangeD <-
dataRarrangeExpD [,c(“correct”,“Circle1”,“Beep1”,“correct_response”,“response”,“subject_nr”]] #data_exp1 $ target_coh& 0
#,DataRearrange $ response,DataRearrange $ correct_response“Response”,“Accuracy” #添加新列以保存所做的选择 DataRearrangeD [c(“RightChoice”,“LeftChoice”,“SimChoice”)] <-
DataRearrangeD $ RightChoice&
ifelse(DataRearrangeD $ response ==“l”,1,0) DataRearrangeD $ LeftChoice <-
ifelse(DataRearrangeD $ response ==“a”,1,0) DataRearrangeD $ SimChoice&
ifelse(DataRearrangeD $ response ==“space”,1,0)
Exp2D.data = DataRearrangeD
#构造报告概率的数据帧 SIM.vis.aud.df = aggregate(SimChoice?Circle1 + Beep1 + subject_nr,data = Exp2D.data,mean) RightFirst.vis.aud.df = aggregate(RightChoice?Circle1 + Beep1 + subject_nr,data = Exp2D.data,mean) LeftFirst.vis.aud .df = aggregate(LeftChoice?Circle1 + Beep1 + subject_nr,data = Exp2D.data,mean)
#组合数据框架 mean.vis.aud.df = data .frame(SIM.vis.aud.df,RightFirst.vis.aud.df $ RightChoice,LeftFirst.vis.aud.df $ LeftChoice) colnames(mean.vis.aud.df)[5:5] = c(“Right”) colnames(mean.vis.aud.df)[6:6] = c(“Left”) colnames 4] = c(“SIM”) colnames(mean.vis.aud.df)[1:2] = c(“Visual”,“Audio”)
$ b b#使用reshape 2,我们将数据帧更改为长格式## measure.var第3列到第5列即3,4,5
mean.vis.aud.long = melt(mean.vis.aud .df,measure.vars = 4:6,variable.name =“Report”,value.name =“Prob”)显示报告的重新排序级别 mean.vis.aud .long $ Report = Relevel(mean.vis.aud.long $ Report,ref = c(“Left”,“SIM”,“Right”)) mean.vis.aud.long $ Visual = Relevel mean.vis.aud.long $ Visual,ref = c(“LeftCust”,“SIMCust”,“RightCust”)) #write.table(mean.vis.aud.long,“C :/ Documents和Settings / psundere / My Documents / Analysis / Exp2_Pilot / reshape.txt“,row.names = F)
############ ################################################## ################################
############## ################################################## #################################### #计算SD,SE均值等 #### ################################################## ########################################
###### ################################################## ######################################
CalSD <- mean.vis.aud.long [,c(“Prob”,“Report”,“Visual”,“Audio”,“subject_nr”)]
#获取平均效果by Prob
CalSD.means <-
aggregate(CalSD [c(“Prob”)], by = CalSD [c(“subject_nr”,“Report”,“Visual”,“Audio”) ],FUN = mean) #“correct”,“Circle1”,“Beep1”,“correct_response”,“response”,“subject_nr” #
CalSD.means $ Prob <-
CalSD.means $ Prob * 100
#获取“Probability”的样本(n-1)标准差 CalSD.sd& ;
aggregate(CalSD.means [“Prob”], by = CalSD.means [c(“Report”,“Visual”,“Audio”)],FUN = sd) $ b b #计算SE
- & SD / sqrt(N) CalSD.se <-
CalSD.sd $ Prob / sqrt(25) SE <-
b $ b#95%的置信区间 - &标准错误* qt(0.975,N-1)SEE帮助(qt)#.975而不是.95因为5%是分配的两侧2.5% ci <SE * qt (0.975,24)
################################# ################################################## ###########
################################### ################################################## #########
##################################### ############## #Bar graph
#mean.vis.aud.long $ Audio&
factor(mean.vis.aud。 long $ Audio,levels = c(“left”,“2centre”,“NoBeep”,“single”,“right”))
AggBar <.aud.long $ Prob * 100, by = list(mean.vis.aud.long $ Report,mean.vis.aud.long $ Visual,mean.vis.aud.long $ Audio),FUN = “mean”) #更改列名称 colnames(AggBar)<-
c(“Report”,“Visual”,“Audio”,“Prob”)
#改变呈现顺序#CondPerRow $ AuditoryCondition <-
factor(CondPerRow $ AuditoryCondition,levels = c(“NoBeep”,“left”,“right”))
prob.bar = ggplot(AggBar,aes(x = Report,y = Prob,fill = Report))+ theme_bw()+ facet_grid b prob.bar + geom_bar(position = position_dodge(.9),stat =“identity”,color =“black”)+ theme(legend.position =“none”)+ labs(x =“Report”,y =报告的概率“)+ scale_fill_grey()+
labs(title = expression(”Visual Condition“))+
theme(plot.title = element_text(size = rel(1)))+
geom_errorbar(aes(ymin = Prob-ci,ymax = Prob + ci), width = .2,#错误条的宽度 position = position_dodge(.9))+
theme(plot.title = element_text(size = rel(1.5)))+
scale_y_continuous(limits = c(0,100),breaks =(seq(0,100,by = 10)))
这是AggBar在生成图形之前操作后的样子:
报告视觉音频制作 1左LeftCust 2中心81.84
2 SIM LeftCust 2中心13.52
3右LeftCust 2中心4.64
4左SIMCust 2centre 17.36
5 SIM SIMCust 2centre 69.76
6右SIMCust 2中心12.88
7左右下角2中心8.88
8 SIM右角2中心13.12
9右右下角2中心78.00
10左左残留左94.48
11 SIM左残余左2.16
12右左残余3.36
13左SIMCust左65.20
14 SIM SIMCust左21.76
15右SIMCust left 13.04
16 Left RightCust left 31.12
17 SIM RightCust left 4.40
18 Right RightCust left 64.48
19左LeftCust NoBeep 66.00
20 SIM LeftCust NoBeep 26.08
21 Right LeftCust NoBeep 7.92
22左SIMCust NoBeep 10.96
23 SIM SIMCust NoBeep 78.88
24右SIMCust NoBeep 10.16
25左右右轮NoBeep 8.48
26 SIM RightCust NoBeep 26.24
27 Right RightCust NoBeep 65.28
28左左侧右侧62.32
29 SIM左侧右侧6.08
30右侧左侧右侧31.60
31左侧右侧17.76
32 SIM SIMCust right 22.16
33 Right SIMCust right 60.08
34 Left RightCust right 5.76
35 SIM RightCust right 3.60
36 Right RightCust right 90.64
37 Left LeftCust single 49.92
38 SIM LeftCust single 47.84
39 Right LeftCust single 2.24
40 Left SIMCust single 6.56
41 SIM SIMCust single 87.52
42 Right SIMCust single 5.92
43 Left RightCust single 3.20
44 SIM RightCust single 52.40
45 Right RightCust single 44.40
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
使用Troy提供的代码,它创建了一个简单的解决方案,在ggplot2中的条形图缺少模式。
这里的代码我用来添加垂直线条以实现基本模式为正确的反应条。我相信你聪明的民众可以适应你自己的需要与纹理/模式虽然基本的:
# ########添加此行以创建高亮后缀 HighlightDataCust <AggBar [AggBar $ Report == gsub(“Cust”,“”,AggBar $ Visual),]
################################################## ###
prob.bar = ggplot(AggBar,aes(x = Report,y = Prob,fill = Report))+ theme_bw()+ facet_grid b $ b prob.bar + geom_bar(position = position_dodge(.9),stat =“identity”,color =“black”)+ theme(legend.position =“none”)+ labs(x =“Response”,y =“Report of Report”)+ scale_fill_grey()+
####################################################################################### ,position = position_dodge(.9),stat =“identity”,color =“black”,size = 2)+
geom_bar(data = HighlightDataCust,position = position_dodge(.9),stat = color =“black”,size = 0.5,width = 0.85)+
geom_bar(data = HighlightDataCust,position = position_dodge(.9),stat =“identity”,color =“black” = 0.65)+
geom_bar(data = HighlightDataCust,position = position_dodge(.9),stat =“identity”,color =“black”,size = 0.5,width = 0.45)+
geom_bar = HighlightDataCust,position = position_dodge(.9),stat =“identity”,color =“black”,size = 0.5,width = 0.25)+
geom_bar(data = HighlightDataCust,position = position_dodge stat =“identity”,color =“black”,width = 0.0)+
############################ #########################
labs(title = expression(“Visual Condition”))+
theme(text = text_text(size = 18))+
theme(axis.title.x = element_text(size = 18))+
theme(axis.title.y = element_text )+
theme(axis.text.x = element_text(size = 12))+
geom_errorbar(aes(ymin = Prob-ci,ymax = Prob + ci), width =。 2,#错误条的宽度 position = position_dodge(.9))+
theme(plot.title = element_text(size = 18))+
scale_y_continuous ,100),breaks =(seq(0,100,by = 10)))
。显然,线可以做成任何你想要的颜色和混合的颜色。只要确保你从最宽的宽度开始,并努力到0.0,所以图层不会覆盖。希望有人发现这有用。 (如果要创建具有不同y轴高度的多个层,也应该可以在条内创建水平线,即每个不同条高的顶部将看起来像一条水平线。没有测试这个自己,但它可能是值得研究的那些需要一个以上的条形图案。结合在一个酒吧应该导致一个网格图案,忘记不同的颜色也可以使用简而言之,我认为这种方法是一个体面的修复缺乏模式在ggplot2中。)
我创建了一个我在这里提到的3种模式的例子:
解决方案 我没有你的数据,所以我使用钻石数据集来演示。
基本上你需要'overplot'第二个 geom_bar() c> data = 属性为仅绘制您要突出显示的条形。只是过滤原始数据排除任何你不想要的。例如下面我们重绘 diamonds [(diamonds $ clarity ==“SI2”),
ggplot(diamonds)+ geom_bar(aes(clarity,fill = color))#first plot d + geom_bar(data = diamonds [(diamonds $ clarity ==“SI2” ),],#filter
aes(clarity),alpha = 0,size = 1,color =“black”)+#plot outline
facet_wrap(?cut)
NB 显然,您的过滤器会更复杂,例如
data = yourdata [(yourdata $ visualcondition ==“LeftCust”& yourdata $ report ==“Left”|
yourdata $ visualcondition ==“SIMCust”& ; yourdata $ report ==“SIM”|
yourdata $ visualcondition ==“RightCust”& yourdata $ report ==“Right”),]
OK已更新您的数据。我必须弥补置信区间,因为它们在AggBar2数据中不可用:
######## #ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
HighlightData& -AggBar2 [AggBar2 $ Report == gsub(“Cust”,“”,AggBar2 $ Visual),]
######## #############################################
prob.bar = ggplot(AggBar2,aes(x = Report,y = Prob,fill = Report))+ theme_bw()+ facet_grid(Audio?Visual) prob.bar + geom_bar(position = position_dodge (x =“Report”,y =“Probability of Report”)+ scale_fill_grey()+ labs
#########添加此行以创建亮点SUBSET
geom_bar(data = HighlightData,position = position_dodge(.9),stat =“identity”,color =“pink”,size = 1)+
##################################### ##################
labs(title = expression(“Visual Condition”))+
theme(plot.title = element_text (size = rel(1)))+
geom_errorbar(aes(ymin = Prob-ci,ymax = Prob + ci), width = .2,#错误条的宽度 position = position_dodge(.9))+
theme(plot.title = element_text(size = rel(1.5)))+
scale_y_continuous(limits = c(0,100),breaks = 0,100,by = 10)))
本文地址: &
扫一扫关注官方微信

我要回帖

更多关于 ggplot 两组数据 的文章

 

随机推荐