ggplot2 并使用图例更改 xlabel 和 ylabel 名称
ggplot2 and change xlabel and ylabel names with also legend
R 脚本的结果下方:
这个 R 代码片段是:
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 的代码片段,我没有绘制任何曲线:
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}$ 但没有成功。
您可以尝试 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")
R 脚本的结果下方:
这个 R 代码片段是:
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 的代码片段,我没有绘制任何曲线:
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}$ 但没有成功。
您可以尝试 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")