Help with histogram/barplot/scatterplot

Hello, I've been trying to generate a plot in R.
Basically, I have a dataset that includes the depth at which certain organism was collected. What I want to do is to graph the depth against the number of individuals. I've tried with histograms and barplots but none seems to work (I'm also a newbie with R).
Plus, I'd like to add a cummulative frequency graph over it just to show how it varies.
I already could do it in Excel, in less than 5 minutes, but as in the rest of my graphs for that work I've used R, I wouldn't like this to be the exception. I didn't do the x-axis numeric in Excel, but it's something else I want in my plot.

Thanks in advance!

UPDATE:
So far I've been able to make the plot as I wanted, I had to use a lollipop plot.

This is my data:

      x  y
1   180  0
2   329  0
3   170  1
4  1530 11
5  1341  1
6   820  1
7  2106  3
8   972  0
9  1222  1
10  947  7
11 1566  5
12  468  0
13 2160  3
14 1800  6
15  220  0

Where 'x' is depth (m) and 'y' is the number of individuals. Then I found a gg plot to make it

h
ggplot(h,
       aes(x = x,
           y = y)) +
  geom_point() +
  geom_segment(aes(x = x,
                   xend = x,
                   y = 0,
                   yend = y))

UPDATE:

I finally could do it!
Here's my code and I'm replying with the plot:

a1 <- c(170, 180, 220, 329, 468, 820, 947, 972, 1222, 1341, 1530, 1566, 2106, 2160)
a2 <- c(1, 6, 0, 0, 0, 1, 7, 0, 1, 1, 11, 5, 3, 3)
a3 <- c(2.56, 17.95, 17.95, 17.95, 17.95, 20.51, 38.46, 38.46, 41.03, 43.59, 71.79, 84.62, 92.31, 100)
Ca <- data.frame(a1, a2, a3)
Ca

ggplot(Ca) + 
  geom_col(aes(x = a1, y = a2), size = 1) +
  geom_line(aes(x = a1, y = 0.1*a3), size = 1, color="red", group = 1, lty = 5) + 
  scale_y_continuous(sec.axis = sec_axis(~./0.1, name = "Frecuencia acumulada (%)")) +
  theme_light() +
  theme(
    panel.grid = element_blank(),
    panel.border = element_blank(),
    axis.line = element_line()
    ) +
  xlab("Profundidad (m)") +
  ylab("Abundancia (número de individuos)")

Lastly, I'd like to know if I can add a legend to make it clearer.

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

So far this is the plot I've been able to do:

Now I'd like to know if I can add a legend.

You can add one manually

library(tidyverse)

Ca <- data.frame(
          a1 = c(170,180,220,329,468,820,947,972,
                 1222,1341,1530,1566,2106,2160),
          a2 = c(1, 6, 0, 0, 0, 1, 7, 0, 1, 1, 11, 5, 3, 3),
          a3 = c(2.56,17.95,17.95,17.95,17.95,20.51,
                 38.46,38.46,41.03,43.59,71.79,84.62,92.31,100)
)

colors <- c("Acumulada" = "red", "Puntual" = "black")

ggplot(Ca) + 
    geom_col(aes(x = a1, y = a2, fill = "Puntual"), size = 1) +
    geom_line(aes(x = a1, y = 0.1*a3, fill = "Acumulada"), color = "red", size = 1, group = 1, lty = 5) + 
    scale_y_continuous(sec.axis = sec_axis(~./0.1, name = "Frecuencia acumulada (%)")) +
    scale_fill_manual(values = colors) +
    theme_light() +
    theme(
        panel.grid = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line()
    ) +
    labs(
        x = "Profundidad (m)",
        y = "Abundancia (número de individuos)",
        fill = "Abundancia"
    )
#> Warning: Ignoring unknown aesthetics: fill

Created on 2021-09-12 by the reprex package (v2.0.1)

That's great! Thank you very much.
I've been reading and I've added some more elements:

ggplot(Ca) + 
  geom_col(aes(x = a1, y = a2, fill = "Puntual"), size = 1) +
  geom_line(aes(x = a1, y = 0.11*a3, fill = "Acumulada"), color = "red", size = 1, group = 1, lty = 5) + 
  scale_x_continuous(breaks = seq(0, 2200, by = 200)) +
  scale_y_continuous(breaks = seq(0, 11, by = 2),
                     sec.axis = sec_axis(breaks = seq(0, 100, by = 20),
                                                      ~./0.11, name = "Abundancia acumulada (%)")) +
  scale_fill_manual(values = colors) +
  theme_light() +
  theme(
    panel.border = element_blank(),
    axis.line = element_line(),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank()
  ) +
  labs(
    x = "Profundidad (m)",
    y = "Abundancia (número de individuos)",
    fill = element_blank()
  )

My doubt now is if I can add a horizontal grid that only intersects at 50%.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.