ggplot2 并用图例更改 xlabel 和 ylabel 名称
Posted
技术标签:
【中文标题】ggplot2 并用图例更改 xlabel 和 ylabel 名称【英文标题】:ggplot2 and change xlabel and ylabel names with also legend 【发布时间】:2021-11-19 07:46:31 【问题描述】:在 R 脚本的结果下方:
这个R代码sn-p是:
as.data.frame(y3) %>%
mutate(row = row_number()) %>% # add row to simplify next step
pivot_longer(-row) %>% # reshape long
ggplot(aes(value, color = name)) + # map x to value, color to name
geom_density()
如何更改xlabel
(值)和ylabel
(密度)的名称以及图例(v1, v2, v3, v4, v5
)?
更新 1
通过使用@Park 的代码 sn-p,我没有绘制曲线:
as.data.frame(y3) %>%
mutate(row = row_number()) %>% # add row to simplify next step
pivot_longer(-row) %>% # reshape long
mutate(name = recode(name, V1="z = 0.9595", V2="z = 1.087", V3="z = 1.2395", V4="z = 1.45", V5="z = 1.688")) %>%
ggplot(aes(value, color = name)) + # map x to value, color to name
geom_density() +
xlab("Distribution of Ratio $b_sp/b_ph$ or each redshift") +
ylab("Number of occurences")
结果:
我也尝试使用 Latex 格式的下标:$b_sp/b_ph$ 但没有成功。
【问题讨论】:
看看?labs
。
【参考方案1】:
你可以试试xlab
,ylab
,scale_color_manual
,
as.data.frame(y3) %>%
mutate(row = row_number()) %>% # add row to simplify next step
pivot_longer(-row) %>% # reshape long
ggplot(aes(value, color = name)) + # map x to value, color to name
geom_density() +
xlab("text") +
ylab("text") +
scale_color_manual(labels = c("a", "b", "c", "d", "e"))
绘图前重新编码
as.data.frame(y3) %>%
mutate(row = row_number()) %>% # add row to simplify next step
pivot_longer(-row) %>% # reshape long
mutate(name = recode(name, V1 = "a", V2 = "b", V3 = "c", V4 = "d", V5 = "e")) %>%
ggplot(aes(value, color = name)) + # map x to value, color to name
geom_density() +
xlab("text") +
ylab("text")
使用Array_total_WITH_Shot_Noise
数据
my_data <- read.delim("D:/Prac/Array_total_WITH_Shot_Noise.txt", header = FALSE, sep = " ")
array_2D <- array(my_data)
z_ph <- c(0.9595, 1.087, 1.2395, 1.45, 1.688)
b_sp <- c(1.42904922, 1.52601862, 1.63866958, 1.78259615, 1.91956918)
b_ph <- c(sqrt(1+z_ph))
ratio_squared <- (b_sp/b_ph)^2
nRed <- 5
nRow <- NROW(my_data)
nSample_var <- 1000000
nSample_mc <- 1000
Cl<-my_data[,2:length(my_data)]#suppose cl=var(alm)
Cl_sp <- array(0, dim=c(nRow,nRed))
Cl_ph <- array(0, dim=c(nRow,nRed))
length(Cl)
for (i in 1:length(Cl))
#(shape/rate) convention :
Cl_sp[,i] <-(Cl[, i] * ratio_squared[i])
Cl_ph[,i] <- (Cl[, i])
L <- array_2D[,1]
L <- 2*(array_2D[,1])+1
# Weighted sum of Chi squared distribution
y3_1<-array(0,dim=c(nSample_var,nRed));y3_2<-array(0,dim=c(nSample_var,nRed));y3<-array(0,dim=c(nSample_var,nRed));
for (i in 1:nRed)
for (j in 1:nRow)
# Try to summing all the random variable
y3_1[,i] <- y3_1[,i] + Cl_sp[j,i] * rchisq(nSample_var,df=L[j])
y3_2[,i] <- y3_2[,i] + Cl_ph[j,i] * rchisq(nSample_var,df=L[j])
y3[,i] <- y3_1[,i]/y3_2[,i]
as.data.frame(y3) %>%
mutate(row = row_number()) %>% # add row to simplify next step
pivot_longer(-row) %>% # reshape long
mutate(name = recode(name, V1="z = 0.9595", V2="z = 1.087", V3="z = 1.2395", V4="z = 1.45", V5="z = 1.688")) %>%
ggplot(aes(value, color = name)) + # map x to value, color to name
geom_density() +
xlab(TeX("Distribution of Ratio $b_sp/b_ph$ or each redshift")) +
ylab("Number of occurences")
【讨论】:
非常感谢!有没有办法像上图一样设置默认值(当我们不使用 scale_color_manual 时)。如果我只设置标签,我会得到一个错误。目前,我必须设置:scale_color_manual(values = c("red", "yellow", "green", "blue", "pink"), labels = c("z = 0.9595", "z = 1. 087", "z = 1.2395", "z = 1.45", "z = 1.688"))
,但颜色不会产生漂亮的颜色。
@youpilat13 我不确定我是否没有查看您的数据y3
,但您可以尝试我在上面添加的代码。如果发生错误,请告诉我。 :D
能否请您看一下我的 UPDATE 1,您的建议有误。问候
@youpilat13 很抱歉输出错误。你能给我一些使用dput
的样本数据吗?或者我可以自己制作样本数据。
别抱歉,还不错。您可以使用位于 ***.com/questions/69246341/… 的代码生成示例 y3
。谢谢以上是关于ggplot2 并用图例更改 xlabel 和 ylabel 名称的主要内容,如果未能解决你的问题,请参考以下文章