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?

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

4 Comments

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.
Thank you. Yes, this trick of +1-1 was unfortunately required to avoid using NULL.
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.
Ah, now I understand. In that case, I'd suggest using tibble::remove_rownames() instead. Much clearer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.