我想将我的数据框的内容存储到没有列名称的.csv文件中.我使用下面的代码,
write.csv(cur_data,new_file, row.names = F, col.names=F)
Run Code Online (Sandbox Code Playgroud)
生成的文件如下所示,
"V1","V2"
-0.02868862,5.442283e-11
-0.03359281,7.669754e-12
-0.03801883,-1.497323e-10
-0.04320051,-6.557672e-11
Run Code Online (Sandbox Code Playgroud)
但是我希望文件格式如下,
-0.02868862,5.442283e-11
-0.03359281,7.669754e-12
-0.03801883,-1.497323e-10
-0.04320051,-6.557672e-11
Run Code Online (Sandbox Code Playgroud)
我不明白为什么不考虑代码中的col.names参数
我在这里 [在.txt文件中] 有一些数据,我读入数据帧df,
df <- read.table("data.txt", header=T,sep="\t")
Run Code Online (Sandbox Code Playgroud)
我使用以下代码删除列中的负值x(因为我只需要正值)df,
yp <- subset(df, x>0)
Run Code Online (Sandbox Code Playgroud)
现在我想在同一层中绘制多个箱形图.我首先融化数据框df,结果图包含几个异常值,如下所示.
# Melting data frame df
df_mlt <-melt(df, id=names(df)[1])
# plotting the boxplots
plt_wool <- ggplot(subset(df_mlt, value > 0), aes(x=ID1,y=value)) +
geom_boxplot(aes(color=factor(ID1))) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +
theme_bw() +
theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+
theme(axis.text=element_text(size=20)) +
theme(axis.title=element_text(size=20,face="bold")) +
labs(x = "x", y = "y",colour="legend" ) +
annotation_logticks(sides = "rl") +
theme(panel.grid.minor = element_blank()) +
guides(title.hjust=0.5) +
theme(plot.margin=unit(c(0,1,0,0),"mm"))
plt_wool …Run Code Online (Sandbox Code Playgroud) 我有以下图,使用此代码生成
plt <- ggplot(d2, aes_string(x=names(same_df)[1],y= "value")) +
geom_point(aes(color = variable), size = 1)+ theme_bw()+
theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+
theme(axis.text=element_text(size=20)) +
theme(axis.title=element_text(size=20,face="bold")) + scale_color_discrete(name = "title", labels = c("1", "2", "3", "4","5","6","7","8","9")) + labs(x = "x", y = "y")+ guides(colour = guide_legend(override.aes = list(size=4),ncol=2,title.hjust=0.5))+theme(plot.margin=unit(c(0,0,0,0),"mm"))
Run Code Online (Sandbox Code Playgroud)

但是我需要在y轴上的数字使用SI前缀表示法,为了得到我做了以下步骤,
library("sos")
Run Code Online (Sandbox Code Playgroud)
在sitools包中使用findFn
findFn("{SI prefix}")
Run Code Online (Sandbox Code Playgroud)
然后我使用标签中的f2si将浮点数编号转换为带有SI前缀的数字
plt2 <- plt + scale_y_continuous(labels=f2si)
Run Code Online (Sandbox Code Playgroud)
结果情节看起来像这样,

当f2si精确地将y轴改变为-1e ^ -0.8到-10n时,它不能准确地显示0和1e ^ -0.8的值,它们分别为0和10n.有人可以建议在这里应该纠正什么,以便数字显示在它们应该贯穿始终.
谢谢.
我有一个数据框x1,它是用以下代码生成的,
x <- c(1:10)
y <- x^3
z <- y-20
s <- z/3
t <- s*6
q <- s*y
x1 <- cbind(x,y,z,s,t,q)
x1 <- data.frame(x1)
Run Code Online (Sandbox Code Playgroud)
我想提取y轴截距和数据的线性回归拟合斜率,
x y z s t q
1 1 1 -19 -6.333333 -38 -6.333333
2 2 8 -12 -4.000000 -24 -32.000000
3 3 27 7 2.333333 14 63.000000
4 4 64 44 14.666667 88 938.666667
5 5 125 105 35.000000 210 4375.000000
6 6 216 196 65.333333 392 14112.000000
7 7 343 323 …Run Code Online (Sandbox Code Playgroud) 我有一个x1我用这段代码生成的数据框,
x <- c(1:10)
y <- x^3
z <- y-20
s <- z/3
t <- s*6
q <- s*y
x1 <- cbind(x,y,z,s,t,q)
x1 <- data.frame(x1)
x y z s t q
1 1 1 -19 -6.333333 -38 -6.333333
2 2 8 -12 -4.000000 -24 -32.000000
3 3 27 7 2.333333 14 63.000000
4 4 64 44 14.666667 88 938.666667
5 5 125 105 35.000000 210 4375.000000
6 6 216 196 65.333333 392 14112.000000
7 7 343 323 107.666667 …Run Code Online (Sandbox Code Playgroud) 我想在ggplot中绘制一个绘图后裁剪出额外的空白.
我有以下代码,
p1 <- ggplot(d2, aes_string(x=names(sameTLM_df)[1],y= "value")) +
geom_point(aes(color = variable), size = 1)+
theme_bw()+
theme(legend.text=element_text(size=18), legend.title=element_text(size=18))+
theme(axis.text=element_text(size=20)) +
theme(axis.title=element_text(size=24,face="bold")) +
scale_color_discrete(name = "Title", labels = c("1","2","3","4","5","6","7","8","9")) + labs(x = "x", y = "y")+
guides(colour = guide_legend(override.aes = list(size=4)))
Run Code Online (Sandbox Code Playgroud)
产生这样的情节,

我想裁剪绘图周围的白色背景,并将图例中的列数更改为2,所以我使用以下代码,
p2 <- p1 + guides(col = guide_legend(ncol = 2)) + theme(plot.background = element_rect(fill = NULL))
Run Code Online (Sandbox Code Playgroud)
这导致了这样的情节,

然而,"空白"(我将背景颜色设为黄色以显示边界和绘图之间的空间)不会被删除,而当列数更改为2时,图例列表的大小也会发生变化,我不知道不需要.有人可以指导我如何保持图例列表的大小,同时将列更改为2并删除绘图外边界中的额外空白.
谢谢
r ×6
ggplot2 ×4
background ×1
boxplot ×1
col ×1
dataframe ×1
divide ×1
legend ×1
outliers ×1
prefix ×1
regression ×1
statistics ×1
whitespace ×1