Make a graph in R (ggplot) similar to Excel bar charts

Asked

Viewed 62 times

0

How can I make a chart in R (ggplot) similar to this excel chart?

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I have a table in form csv with this data. I got up to the R this table, but now I don’t know how to make this graph using the R.

the table link: https://drive.google.com/file/d/1RqQSlupktWMqAD44xs9GgT0UXPyhdnU6/view?usp=sharing

Update: Follow the table data in separator text format ";":

Estudo;Vegetação_Nativa;Pastagem;Corpo_Hídrico;Solo_Exposto;Silvicultura;Agricultura
GEOMS 2007;15,71;83,99;0,31;0;0;0
Plano de Manejo 2008;16,99;82,69;0,31;0;0,02;0
Garcia et. al, (2014) - 2011;15,74;76,27;0,3;4,11;3,55;0
Oliveira et. al. (2017);18,24;23,8;0,3;49,47;8,2;0
Finck et al. (2020) - 2018;22,63;62;0,4;6,69;8,28;0
MapBiomas 2019;14,9;75,34;0,26;0,06;8,2;1,13
CART - Sentinel2 2020;21,39;67,89;0,24;0;10,56;0
  • Welcome to [en.so]! You have posted an image of the code and/or error message. Although it sounds like a good idea, it’s not! One of the reasons is that if someone wants to answer the question, they can’t copy the code and change something inside. Click on the [Edit] link and put the code/error as text. See more about this in these links - Manual on how NOT to ask questions, Post Error Message as Picture

  • Hello Lucas, I didn’t actually put code. the first image refers to the table I have. the second image is where I want to go, but I don’t know how to do it in R. abçs

  • Yes. To replicate your problem it would be ideal if you provided the data in text format

1 answer

4


library(dplyr)
library(tidyr)
library(ggplot2)

df <- read.csv2('./Tabela_areas_referencias_porcent_2.csv')

df_pivoted <- pivot_longer(
  data = df,
  cols = c("Vegetação_Nativa", "Pastagem","Corpo_Hídrico",
           "Solo_Exposto","Silvicultura","Agricultura"),
  values_to = 'Valores',
  names_to = 'Vegetacao'
)

df_pivoted %>%
  ggplot(aes(Estudo, Valores, fill = Vegetacao)) +
  geom_bar(stat = 'identity', position = 'dodge')+
  theme(axis.text.x = element_text(angle = 15, hjust=1))

You can pivot the data and use ggplot.

  1. load the data
  2. we create the pivot data.frame
  3. plot the graph

graph

  • ola lmonferrari, I tested your code here swirled right here. Would you have any way to change colors? thanks for your help.

  • 1

    You can use the scale_fill_manual, after Theme you add a + and add this line: scale_fill_manual(values = c('#D6F9DD','#99F7AB','#ABDF75','#60695C','#C0D7BB','#644536')). You can change the Hex codes to the colors that please you. Hug!

Browser other questions tagged

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