Home > Net >  Convert geom_point() graph into a bar graph
Convert geom_point() graph into a bar graph

Time:07-28

I am trying to make a bar plot showing different elements, but I am only managing to make it with dots, I'd like to have it with bars. Tried joining different individual graphs but it does not look very good.

data:

1 primary    wet     aver_ind/day   9.810680
2 primary    wet      aver_sp/day   7.215686
3 primary    wet aver_biomass/day 262.346116
4 primary    wet          fishers  38.762760
5 primary    dry     aver_ind/day  10.633333
6 primary    dry      aver_sp/day   7.008333

Joining individual graphs:

aver_ind.day<-ggplot(graphics, mapping = aes(x=forest, y =aver_ind.day))   geom_col()
aver_sp.day<-ggplot(graphics, mapping = aes(x=forest, y =aver_sp.day))   geom_col()
aver_biomass.day <-ggplot(graphics, mapping = aes(x=forest, y =aver_biomass.day ))   geom_col()
fishers<-ggplot(graphics, mapping = aes(x=forest, y =fishers))   geom_col()
library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(aver_ind.day), ggplotGrob(aver_sp.day), 
                ggplotGrob(aver_biomass.day), ggplotGrob(fishers), size = "last")) 

This is what I'd like to reproduce in bars rather than dots:

index<- ggplot(graphics, aes(x= factor(season,
 level=c("wet", "dry")), y=value, size = 5,
color=season, shape=forest))   
  geom_point() 
  labs(
    title = "Index ",
    subtitle = "Data from 2004 to 2020") 
  
  theme( axis.title.x = element_text(size = 15),
         axis.title.y = element_text(size = 15),
         axis.text = element_text(size =20)) 
  theme_bw() 
  theme(axis.title.x = element_text(size = 18),
        axis.title.y = element_text(size = 18),
        axis.text = element_text(size =18)) 
  theme() 
  theme(panel.grid.major = element_blank(), # remove the major lines
        panel.grid.minor = element_blank())  
  labs(x= "Forest",
       y= "Value") 

final_plot<-  deforest4   scale_shape_discrete( labels = c("aver_ind/day ", "aver_sp/day", "aver_biomass/day ", "fishers")) 
  scale_colour_discrete(labels = c("aver_ind/day ", "aver_sp/day", "aver_biomass/day ", "fishers")) 
  theme(legend.text = element_text(size=15)) 
  guides(colour = guide_legend(override.aes = list(size=8))) 
  theme(legend.title = element_text(size=25)) 
  theme(legend.position = "none") 
  
  facet_grid(index~ forest, scales = "free")

Ugly graph

dput(graphics)
structure(list(forest = c("primary", "primary", "primary", "primary", 
"primary", "primary", "primary", "primary", "secondary", "secondary", 
"secondary", "secondary", "secondary", "secondary", "secondary", 
"secondary"), season = c("wet", "wet", "wet", "wet", "dry", "dry", 
"dry", "dry", "wet", "wet", "wet", "wet", "dry", "dry", "dry", 
"dry"), index = c("aver_ind/day", "aver_sp/day", "aver_biomass/day", 
"fishers", "aver_ind/day", "aver_sp/day", "aver_biomass/day", 
"fishers", "aver_ind/day", "aver_sp/day", "aver_biomass/day", 
"fishers", "aver_ind/day", "aver_sp/day", "aver_biomass/day", 
"fishers"), value = c(9.810679612, 7.215686275, 262.3461165, 
38.76276, 10.633333, 7.008333333, 276.1975, 36.59324, 15.28125, 
6.979166667, 193.825, 32.70533, 17.07079646, 7.601769912, 202.4433628, 
35.73062)), class = "data.frame", row.names = c(NA, -16L))

CodePudding user response:

Update: We some tweaking: enter image description here

library(tidyverse)

graphics %>% 
  ggplot(aes(x = season, y=value, fill=season))  
  geom_col(width = 0.6) 
  facet_grid(index ~ forest,  scales = "free_y") 
  scale_fill_manual(values = c("maroon", "gold")) 
  theme_bw()

First answer: Do you mean something like this?

library(tidyverse)

graphics %>% 
  ggplot(aes(x = season, y=value, fill=season))  
  geom_col() 
  facet_grid(index ~ forest)

enter image description here

CodePudding user response:

# Example data
library(ggplot2)
set.seed(123)

df <- data.frame(
  forest = rep(c("wet", "dry"), each = 8),
  value = sample(1:16, replace = F),
  primary = rep(c("primary", "secondary"), 8),
  type = rep(rep(c("biomass","ind","sp","fishers"), each = 2), 2)
)


# Plot
ggplot(df, aes(x = forest, y = value, fill = forest))   
  geom_col()   
  theme_bw()  
  facet_grid(type ~ primary)  
  ggtitle("Index")

  • Related