Making a Geom_bar plot by using ggplot

I wanna make a bar chart using data from screenshots. Here Countries_Name column is used in Y-axis, where EP and NP. go for X axia. Could you help me to do this?

Are you looking for something like this?

library(tidyverse)

dat <-
  tibble::tribble(
  ~Country_Name,  ~EP,  ~NP,
           "EU", 39.6, 33.2,
      "Belgium",   38,   42,
      "Denmark",   46, 41.3
  )

dat_long <-
  dat %>% 
  pivot_longer(cols = c("EP", "NP"),
               names_to = "variable",
               values_to = "value")

dat_long %>% 
  ggplot(aes(x = variable,
             y = value)) +
  geom_bar(stat = "identity") +
  facet_grid(Country_Name ~ .)

Rplot

It would be like my photo. If you need I can give you excel data

How's this?

library(tidyverse)

dat <-
  tibble::tribble(
    ~Country_Name,  ~EP,  ~NP,
    "EU", 39.6, 33.2,
    "Belgium",   38,   42,
    "Denmark",   46, 41.3
  )

dat_long <-
  dat %>% 
  pivot_longer(cols = c("EP", "NP"),
               names_to = "variable",
               values_to = "value")

dat_long %>% 
  ggplot(aes(x = reorder(Country_Name, desc(Country_Name)),
             y = value,
             fill = variable)) +
  geom_col(position = "dodge") +
  coord_flip()

Rplot01

1 Like

Its cool right now. Now I am trying to apply this

It's really helpful to your comment. At last I made it

1 Like

Hi @Kader_Zone - is your x axis correctly formatted as numeric data? In your picture the difference between 0 and 13.1 is the same as the distance between 13.1 and 13.4.

If the column isn't numeric, you could change it by doing something like

dat$col = as.numeric(dat$col)

Or you could use dplyr::mutate() instead.

This topic was automatically closed 21 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.