<p>I have used the <code>seewave</code> package with <code>ggplot</code> very successfully.  I needed to tweak the values a bit depending on the file you are using.  Here is an example code.  you will need to point it to your own wav file.</p>
<pre><code># Make a spectrogram in R
# 
# This script makes a spectrogram for a single sound file
# 
# Author: Abram F.
# Date Created: 11 Sep 2019
# Date Last Updated 11 Sep 2019

library(tuneR)
library(seewave)
library(dplyr)
library(ggplot2)

sound_path&lt;- &quot;C:/Users/cc/Downloads/MANO7_20220928_MANO7_20220919_043500_0_startsec_38_label_possBAOW (1).wav&quot;
save_path&lt;-&quot;test.png&quot;



# Load sound (only loading first 10 seconds)
s&lt;-tuneR::readWave(sound_path,from =1,to = 3.5,units = &quot;seconds&quot;)

# load the spectrogram
sound_data &lt;- seewave::ggspectro(s,
                                 norm = T, 
                                 flim=c(0.1,10), # frequency limits, kHz 
                                 wl = 512, # fft window length (start w/ 1024 or 512 )
                                 ovlp = 80) # % window overlap, x direction


# get the spectrogram data to darken the background and make nice contrast
hmm&lt;-seewave::spectro(s,plot=F) 

# get the high and low contrast numbers to cut out 
top =quantile(hmm<span class="math-container">$amp,probs = .99)
bottom = quantile(hmm$</span>amp,probs = 0.4)

# reset all values above the top value to the top and below the bottom value
sound_data<span class="math-container">$data$</span>amplitude_cut &lt;- dplyr::case_when(sound_data<span class="math-container">$data$</span>amplitude&gt;top ~top ,
                                                  sound_data<span class="math-container">$data$</span>amplitude&lt;bottom ~bottom,
                                                  TRUE ~ sound_data<span class="math-container">$data$</span>amplitude)



# print the plot

sound_data+
  geom_raster(aes(fill = amplitude_cut)) + 
  scale_fill_viridis_c(option = 'plasma',
                       na.value = plasma(n = 100)[1])+
  scale_x_continuous(breaks= seq(0,10,1),minor_breaks = seq(0,10,.25))+
  scale_y_continuous(breaks= seq(0,10,1),minor_breaks = seq(0,10,.25))+
  theme_classic()+
  theme(legend.position = 'none')

ggsave(gsub(&quot;.wav&quot;,&quot;.png&quot;,sound_path),height = 3,width = 6,units = &quot;in&quot;,dpi = 150)

<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2023-04-07T20:35:12.687Z
2023-04-07T20:35:12.687Z
Abram Fleishman
<p>The seewave package allows you to create spectrograms in R, and includes the ggspectro function which allows you to use ggplot to plot spectrograms. You can find examples here <a href="https://rug.mnhn.fr/seewave/" rel="noreferrer">https://rug.mnhn.fr/seewave/</a></p>

CC BY-SA 4.0
2023-04-04T10:23:41.363Z
2023-04-04T10:23:41.363Z
DeniseR
<p>My question is related to <a href="https://bioacoustics.stackexchange.com/questions/1156/preferred-r-code-settings-packages-for-publication-level-spectrograms">this one</a><br />
I'm looking for a way of plotting beautiful spectrograms that would be compatible with ggplot2 in R, so that I can add some lines, points, annotations, or control font size etc.<br />
Does someone know what package could do that / if this exist?</p>

<r><spectrogram>
Spectrograms with ggplot2?
# Spectrograms with ggplot2?

(Asked by: username_0 on 2023-04-03)

My question is related to [this one](https://bioacoustics.stackexchange.com/questions/1156/preferred-r-code-settings-packages-for-publication-level-spectrograms)  
I'm looking for a way of plotting beautiful spectrograms that would be compatible with ggplot2 in R, so that I can add some lines, points, annotations, or control font size etc.  
Does someone know what package could do that / if this exist?

**Tags:** r, spectrogram


---

## 2 answers

### Answer by username_2 on 2023-04-04. Score: 8 | Accepted answer

The seewave package allows you to create spectrograms in R, and includes the ggspectro function which allows you to use ggplot to plot spectrograms. You can find examples here <https://rug.mnhn.fr/seewave/>


---

### Answer by username_1 on 2023-04-07. Score: 6

I have used the `seewave` package with `ggplot` very successfully. I needed to tweak the values a bit depending on the file you are using. Here is an example code. you will need to point it to your own wav file.

```
# Make a spectrogram in R
# 
# This script makes a spectrogram for a single sound file
# 
# Author: username_1 F.
# Date Created: 11 Sep 2019
# Date Last Updated 11 Sep 2019

library(tuneR)
library(seewave)
library(dplyr)
library(ggplot2)

sound_path<- "C:/Users/cc/Downloads/MANO7_20220928_MANO7_20220919_043500_0_startsec_38_label_possBAOW (1).wav"
save_path<-"test.png"

# Load sound (only loading first 10 seconds)
s<-tuneR::readWave(sound_path,from =1,to = 3.5,units = "seconds")

# load the spectrogram
sound_data <- seewave::ggspectro(s,
                                 norm = T, 
                                 flim=c(0.1,10), # frequency limits, kHz 
                                 wl = 512, # fft window length (start w/ 1024 or 512 )
                                 ovlp = 80) # % window overlap, x direction

# get the spectrogram data to darken the background and make nice contrast
hmm<-seewave::spectro(s,plot=F) 

# get the high and low contrast numbers to cut out 
top =quantile(hmm$amp,probs = .99)
bottom = quantile(hmm$amp,probs = 0.4)

# reset all values above the top value to the top and below the bottom value
sound_data$data$amplitude_cut <- dplyr::case_when(sound_data$data$amplitude>top ~top ,
                                                  sound_data$data$amplitude<bottom ~bottom,
                                                  TRUE ~ sound_data$data$amplitude)

# print the plot

sound_data+
  geom_raster(aes(fill = amplitude_cut)) + 
  scale_fill_viridis_c(option = 'plasma',
                       na.value = plasma(n = 100)[1])+
  scale_x_continuous(breaks= seq(0,10,1),minor_breaks = seq(0,10,.25))+
  scale_y_continuous(breaks= seq(0,10,1),minor_breaks = seq(0,10,.25))+
  theme_classic()+
  theme(legend.position = 'none')

ggsave(gsub(".wav",".png",sound_path),height = 3,width = 6,units = "in",dpi = 150)

```

```


---

