1

Below the result of a R script:

Image generated

This R code snippet is:

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() 

How can I change the name of xlabel (value) and ylabel (density) and the legend also (v1, v2, v3, v4, v5)?

Update 1

By using the code snippet of @Park, I get no curves plotted:

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")

and the result:

no curves

I tried also to use subscript with Latex format : $b_{sp}/b_{ph}$ but without success.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

You may try 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"))

Recode before plot

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") 

Using Array_total_WITH_Shot_Noise data

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")

myplot

Park
  • 14,771
  • 6
  • 10
  • 29
  • Thanks a lot ! Is there a way to set the default values (when we don't use the scale_color_manual) like on my figure above. If I set only labels, I get an error. For the moment, I am obliged to set : `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"))` but colors don't make pretty colors. –  Sep 27 '21 at 07:13
  • @youpilat13 I'm not sure with this way that I didn't look at your data `y3`, but you may try code I add above. Please let me know if that occurs an error. :D – Park Sep 27 '21 at 07:19
  • Could you take a look please at my **UPDATE 1**, I get errors with your suggestion. Regards –  Sep 27 '21 at 08:09
  • @youpilat13 I'm sorry for wrong output. Would you give me some sample data using `dput`? Or I can make sample data by myself. – Park Sep 27 '21 at 08:10
  • don't be sorry, it is not bad. You can generate the sample `y3` with the code located on : https://stackoverflow.com/questions/69246341/r-script-make-multiple-plots-with-ggplot2 . thanks –  Sep 27 '21 at 08:15
  • @youpilat13 Thanks a lot! I'll take a look with your `Array_total_WITH_Shot_Noise` file. – Park Sep 27 '21 at 08:19
  • the five curves are computed from column 2 to 6 of Array_total_WITH_Shot_Noise –  Sep 27 '21 at 08:23
  • @youpilat13 It's weird that it give me density curve. Does something wrong with my code above? – Park Sep 27 '21 at 08:26
  • @youpilat13 Furthermore, you can use LaTex format using `library(latex2exp)` `TeX` function, like `xlab(TeX("Distribution of Ratio $b_{sp}/b_{ph}$ or each redshift"))` – Park Sep 27 '21 at 08:30
  • Great it works ! just a last precision : how to replace "`name`" by "`redshift`" at the right of the figure (above the color boxes) ? –  Sep 27 '21 at 08:56
  • @youpilat13 Simply add `+ labs(colour = "text")` – Park Sep 27 '21 at 09:00