1

I want to use automatic index for x-axis in ggplot2. My data set is followed:

library(tidyverse)
library(ElemStatLearn)
phoneme <- as_tibble(phoneme)
aa = phoneme %>% 
  filter(g == "aa")

This is a phoneme data, recording 256 frequencies for each 695 data.

With base code, I can do as follows: (Let's do only 15 of 695 data)

aa[1:15, 1:256]
min_l = min( aa[1:15, 1:256] )
max_l = max( aa[1:15, 1:256] )

ii=1
plot( as.double(aa[ii, 1:256]), ylim=c(min_l,max_l), type="l", col="green", xlab="Frequency")
for( ii in 2:15 ){
  lines( as.double(aa[ii,]), col="green" )
}

But when I try to do it using ggplot2, I'm confused.

What should I put in aes?

ggplot(data = aa, aes(x = 1:256, y = aa[1, 1:256])) + geom_line()

returns an error. How can I deal with it?

HyeonPhil Youn
  • 428
  • 4
  • 11

1 Answers1

1
library(tidyverse)
library(ElemStatLearn)
phoneme <- as_tibble(phoneme)
aa = phoneme %>% 
  filter(g == "aa")

aa[1:15, 1:256]
min_l = min( aa[1:15, 1:256] )
max_l = max( aa[1:15, 1:256] )

ii=1
plot( as.double(aa[ii, 1:256]), ylim=c(min_l,max_l), type="l", col="green", 
xlab="Frequency")
for( ii in 2:15 ){
  lines( as.double(aa[ii,]), col="green" )
}

enter image description here

library(reshape2)
aa2 <- aa %>% 
  dplyr::slice(1:15) %>%  
  dplyr::select(-g, -speaker) %>% 
  t %>% as.data.frame() %>% 
  dplyr::add_rownames() %>% 
  dplyr::select(-rowname) %>% 
  dplyr::mutate(id = 1:256) %>% 
  reshape2::melt(id.vars = "id")

ggplot2::ggplot(aa2) + 
  geom_line(aes(x = id, y = value, col = variable), show.legend = F) +
  scale_x_continuous(breaks = seq(0, 250, 50)) +
  scale_y_continuous(limits = c(min_l,max_l)) +
  scale_color_manual(values = rep("green", 256)) +
  xlab("Frequency") +
  theme_classic()

enter image description here


Comment:

When manipulated the dataframe to perform the transposed matrix, the object under manipulation acquires the names of the varibles in each row (rownames). So, to make the plot easier and make the df more elegant I think it is interesting to remove the names of the rows.

So it was necessary at first to include the names in the df and (dplyr::add_rownames()) later to remove the column with the names of the rows (dplyr::select(-rowname)) .

This gives a false illusion of error, but I performed in a redundant way to avoid using NULL. See link.


Editing by Gregor's comment:

aa2 <- aa %>% 
  dplyr::slice(1:15) %>%  
  dplyr::select(-g, -speaker) %>% 
  t %>% as.data.frame() %>% 
  tibble::remove_rownames() %>% # Comment
  dplyr::mutate(id = 1:256) %>% 
  reshape2::melt(id.vars = "id")
bbiasi
  • 1,549
  • 2
  • 15
  • 31
  • 2
    Nice answer! But what's the point of `add_rownames %>% select(-rowname)`, seems like `x + 1 - 1`. And `add_rownames` is deprecated in current versions, recommending `tibble::rownames_to_column` instead. – Gregor Thomas May 01 '19 at 03:05
  • Thank you. Yes, this trick of `+1-1` was unfortunately required to avoid using `NULL`. – bbiasi May 01 '19 at 03:16
  • I haven't run the code to see, but I don't understand what you mean by "avoid using `NULL`. I think it would at least be worth a comment/explanation in the answer, since at a glance it looks like a mistake. – Gregor Thomas May 01 '19 at 03:34
  • Ah, now I understand. In that case, I'd suggest using `tibble::remove_rownames()` instead. Much clearer. – Gregor Thomas May 01 '19 at 04:24