我有代码创建一个boxplot,使用R中的ggplot,我想用年份和Battle标记我的异常值.
这是我创建箱图的代码
require(ggplot2)
ggplot(seabattle, aes(x=PortugesOutcome,y=RatioPort2Dutch ),xlim="OutCome",
y="Ratio of Portuguese to Dutch/British ships") +
geom_boxplot(outlier.size=2,outlier.colour="green") +
stat_summary(fun.y="mean", geom = "point", shape=23, size =3, fill="pink") +
ggtitle("Portugese Sea Battles")
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?我知道这是正确的,我只想标记异常值.
我一直试图使用facet_grid一起生成一系列点图.这样做,我注意到geom_dotplot似乎没有响应facet_grid的scales ="free_y"参数.
这是一些示例代码:
require(ggplot2)
#Example data
set.seed(3)
df = data.frame(Gene = rep(c("a", "b", "c", "d"), each=20),
ToD = rep(c("Morning", "Evening"), times = 40),
Expression = c(runif(20, min=0, max=10),
runif(20, min=0, max=1),
runif(20, min=0, max=1000),
runif(20, min=0, max=100)))
#Box plots of example data
ggplot(df, aes(x = ToD, y = Expression)) +
geom_boxplot() +
facet_grid(Gene ~ ., scales = "free_y")
#Dot plots of example data
ggplot(df, aes(x = ToD, y = Expression)) +
geom_dotplot(binaxis = "y", stackdir = "centerwhole") +
facet_grid(Gene ~ …
Run Code Online (Sandbox Code Playgroud) 这应该相当容易,但我找不到我的方式.
tri_fill <- structure(
list(x= c(0.75, 0.75, 2.25, 3.25),
y = c(40, 43, 43, 40)),
.Names = c("x", "y"),
row.names = c(NA, -4L), class = "data.frame",Integrated=NA, Related=NA)
# install.packages("ggplot2", dependencies = TRUE)
require(ggplot2)
ggplot(data=tri_fill,aes(x=x, y=y))+
geom_polygon() +
scale_fill_gradient(limits=c(1, 4), low = "lightgrey", high = "red")
Run Code Online (Sandbox Code Playgroud)
我想要的是沿着x轴的渐变,但是上面我只得到一个带有渐变的图例和带有实心填充的多边形.
ggplot2可以创建一个非常有吸引力的填充小提琴情节:
ggplot() + geom_violin(data=data.frame(x=1, y=rnorm(10 ^ 5)),
aes(x=x, y=y), fill='gray90', color='black') +
theme_classic()
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我想将填充限制在95%的分布中心,保持轮廓完整.有没有人有关于如何做到这一点的建议?
波浪号表达式在我的 RStudio 上不起作用!我正在使用 Windows 10 操作系统。
更新:仔细查看后(感谢 Dason)似乎“~”指向“用户/文档/”而不是“用户/”。有什么解决办法吗?
有什么帮助吗?
我想更改此条形图中“丰富”一词的位置。我希望该单词与其他标签的第一个单词的位置对齐,即放置在顶行而不是底行。
p0 <-
ggplot(lineshift, aes(x=name, y=value, fill=factor(product))) +
geom_bar(stat="identity", position="dodge", width=0.8) +
geom_text(aes(x=name, y=value, ymax=value, label=round(value),
hjust=0.5, vjust=-0.5),
position = position_dodge(width=0.8)) +
scale_fill_manual(values=c("blue", "red"),
name="Treatment",
breaks=c(1, 2),
labels=c("A", "B")) +
xlab("Biodiversity") +
ylab("Relative occurences (%)") +
scale_y_continuous(expand=c(0.0,0.5),
limits=c(0, 71),
breaks=c(0,10,20,30,40,50,60,70),
labels=c("0","10","20","30","40","50","60","70")) +
scale_x_discrete(limits=c("Biodiversity of tropical lowland rainforest", "Species composition in understorey", "Richness"),
labels = sapply(strwrap(as.character(lineshift$name), width=30, simplify=F), paste, collapse="\n")) +
theme_bw() +
theme(legend.position=c(0.9,0.9)) +
theme(legend.title=element_blank()) +
theme(axis.title.x = element_text(vjust=0.1,face="bold", size=16),
axis.text.x = element_text(vjust=0.1, size=14, angle=0))+
theme(axis.title.y = element_text(angle=90, vjust=0.70, face="bold", …
Run Code Online (Sandbox Code Playgroud) 我使用data.table来存储数据.我想弄清楚每行中的某些列是否唯一.我想在data.table中添加一个列,如果存在重复值,则将保留值"Duplicated Values",如果没有重复值,则为NA.我想要检查重复的列的名称存储在字符向量中.例如,我创建了我的data.table:
tmpdt<-data.table(a=c(1,2,3,4,5), b=c(2,2,3,4,5), c=c(4,2,2,4,4), d=c(3,3,1,4,5))
> tmpdt
a b c d
1: 1 2 4 3
2: 2 2 2 3
3: 3 3 2 1
4: 4 4 4 4
5: 5 5 4 5
Run Code Online (Sandbox Code Playgroud)
我有另一个变量,指示我需要检查哪些列重复.重要的是,我能够将列名存储在字符向量中,而不需要"知道"它们(因为它们将作为参数传递给函数).
dupcheckcols<-c("a", "c", "d")
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
> tmpdt
a b c d Dups
1: 1 2 4 3 <NA>
2: 2 2 2 3 Has Dups
3: 3 3 2 1 <NA>
4: 4 4 4 4 Has Dups
5: 5 5 4 5 …
Run Code Online (Sandbox Code Playgroud) 我想用两种颜色制作我的报告.我想知道ggplot2中的平滑曲线的默认颜色是什么,所以我可以相应地命名我的bar/line/pie.谢谢.
我正在尝试创建一个多项目时间表,其中包含项目的各个阶段,以便在一个易于可视化的地方组织我们当前的所有项目。
我最初找到了这个timeline()
包,并且能够半创建我所希望的。然而,这个包似乎不是很可定制,所以我希望能完成我的时间线ggplot2()
。
这是我的假数据:
Phase Project StartDate EndDate
SD Test1 2015-08-01 2015-08-31
DD Test1 2015-08-31 2015-09-30
CD Test1 2015-09-30 2015-11-14
PC Test1 2015-11-14 2015-12-14
CA Test1 2015-12-14 2016-08-10
SD Test2 2015-09-01 2015-10-01
DD Test2 2015-10-01 2015-10-31
CD Test2 2015-10-31 2015-12-15
PC Test2 2015-12-15 2016-01-14
CA Test2 2016-01-14 2017-01-08
SD Test3 2016-01-01 2016-01-13
DD Test3 2016-01-13 2016-01-25
CD Test3 2016-01-25 2016-02-12
PC Test3 2016-02-12 2016-03-13
CA Test3 2016-03-13 2017-01-07
SD Test4 2015-06-01 2015-06-01
DD Test4 2015-06-01 2015-06-01
CD …
Run Code Online (Sandbox Code Playgroud) 我是新手,但有一个问题:
我有一个包含15列和33,000行的数据集(csv文件)。
当我在Excel中查看数据时看起来不错,但是当我尝试将数据加载到R-studio时出现问题:
我使用了代码:
x <- read.csv(file = "1energy.csv", head = TRUE, sep="")
View(x)
Run Code Online (Sandbox Code Playgroud)
结果是列名很好,但是数据(第2行及以后的数据)都在我的第一列中。
在第一列中,数据用分隔。。但是当我尝试代码时:
x1 <- read.csv(file = "1energy.csv", head = TRUE, sep=";")
Run Code Online (Sandbox Code Playgroud)
下一个问题是:read.table中的错误(文件=文件,标头=标头,sep = sep,引用=引号,:不允许重复的“ row.names”
所以我做了代码:
x1 <- read.csv(file = "1energy.csv", head = TRUE, sep=";", row.names = NULL)
Run Code Online (Sandbox Code Playgroud)
看起来好像可以正常工作...。但是现在数据位于错误的列中(例如,“名称”列现在包含“时间”值,而“时间”列包含“成本”值)。
有人知道如何解决此问题吗?我可以重命名列,但我认为这不是最好的方法。