ggplot appears empty: data does not appear

Asked

Viewed 81 times

2

I want to create a `ggplot with the frequencies at which the detections of a species of fish appear. But when I run, plot the graph without the detection frequency data, the frame is empty. How to solve?

Here’s some data for playback:

ID          Freq
               0
Ana          390
Boca        2764
Joana          3
Cabelo      3276
Fabiane       10

table(myc_t$ID)->det_id
as.data.frame(det_id)->det_id
names(det_id)<-c("ID","Freq")
head(det_id)

library(ggplot2)

ggplot(det_id, aes(x = "ID", y = "Freq")) +
  geom_bar(stat = "identity") +
  scale_x_discrete(limits = c("Ana", "Boca", "Joana", "Cabelo", "Fabiane", "Manu")) +
  labs(x = "Nomes dos indivíduos marcados", y = "Frequencia de detecção") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Generates graph, but no values and error message appears:

Warning message: Removed 16 Rows containing Missing values (position_stack).

Dice

det_id <- read.table(text = "
ID          Freq
''             0
Ana          390
Boca        2764
Joana          3
Cabelo      3276
Fabiane       10
", header = TRUE)
  • 4

    Try to correct aes(x="ID",y="Freq") for aes(x = ID,y = Freq). That’s a good start.

  • @Rumenickpereira With this correction it worked the first time.

  • Yes, I said it is a good start, because it is a new contributor. Rsrs.

  • @Noisy, can signal that is a useful comment, please.

  • @Rumenickpereira I don’t understand, signal your comment? I already did.

  • @Noisy, sorry, I hadn’t seen it yet.

  • Just a hint: you can use geom_col(), which is a alias for geom_bar(stat = "identity")

  • It worked, guys! Thank you!

  • Just something strange in the result. The number of detections came out different from the matrix table. The individuals "Fabiane" and "Manu" for example have many detections, but when I ask to read the amount of head(det_id) detections the result is much smaller. (Obs. The Matrix table has more than 50,000 rows).

Show 4 more comments

2 answers

4

As mentioned in the first comment, simply remove the ID and Freq quotes.

Then we would have:

ggplot(det_id, aes(x = ID, y = Freq)) +
    geom_bar(stat = "identity") +
    scale_x_discrete(limits = c("Ana", "Boca", "Joana", "Cabelo", "Fabiane", "Manu")) +
    labs(x = "Nomes dos indivíduos marcados", y = "Frequencia de detecção") +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

inserir a descrição da imagem aqui

To learn some lesson from it, because it happened?

Well the uses what we call Non-standard Evaluation (NSE) which is, to put it bluntly, the capacity of the R to read the text of the past code and not its contents. Within the whole it is used because it allows typing less eliminate decessity of i) using quotes and ii) repeatedly referring to the initial object (ID in place of det_id$ID or det_id[["ID"]] or even "ID").

To use the standard Evaluation (if needed in any context) you can use the function aes_string()

ggplot(det_id, aes_string(x = "ID", y = "Freq")) +
        geom_bar(stat = "identity") +
        scale_x_discrete(limits = c("Ana", "Boca", "Joana", "Cabelo", "Fabiane", "Manu")) +
        labs(x = "Nomes dos indivíduos marcados", y = "Frequencia de detecção") +
        theme(axis.text.x = element_text(angle = 90, vjust = 0.5)

More information about NSE can be found here.

-1

ggplot(det_id, aes(x = ID, y = Freq)) +
    geom_bar(stat = "identity") +
    scale_x_discrete(limits = c("Ana", "Boca", "Joana", "Cabelo", "Fabiane", "Manu")) +
    labs(x = "Nomes dos indivíduos marcados", y = "Frequencia de detecção") +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.