Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot two barplot in R

Tags:

r

bar-chart

 data <- as.matrix(data.frame("A" = c(18,7),
                                "B+" = c(3,2),
                                "B" = c(3,3),
                                "C+" = c(6,0),
                                "C" = c(7,0),
                                "D" = c(0,4),
                                "E" = c(5,23)))
    
    
    barplot (data,
            col = c("red","blue"),
            beside = TRUE,
            xlab = "Grade",
            ylab = "Frequency")
    
    
    legend("topleft",
           c("IFC6503-A","IFC6510"),
           fill = c("red","blue"),
           inset = c(.01,0)
    )

I plot two barplot together, the barplot it contain grade, from two different data, the grade is by A B+ B C+ C D E , but the result B+ and C+ is not appear, just appear B. and C. , is my code is wrong or can you guys correct my code?

like image 669
Jonathan Pangkey Avatar asked Sep 18 '21 06:09

Jonathan Pangkey


People also ask

How do I make a stacked barplot in R?

In order to create a stacked bar chart, also known as stacked bar graph or stacked bar plot, you can use barplot from base R graphics. Note that you can add a title, a subtitle, the axes labels with the corresponding arguments or remove the axes setting axes = FALSE , among other customization arguments.

What is multiple bar diagram?

Answer : Multiple bar diagrams are those diagrams which show two or more sets data simultaneously. Generally, these diagrams are used to make comparison between two sets of series, such as birth-rate series. Solution : Multiple bar diagrams are those diagrams which show two or more sets data simultaneously.

How to create bar plots in R?

When a variable takes a few values, it is common to summarize the information with a frequency table that can be represented with a barchart or barplot in R. In this article we are going to explain the basics of creating bar plots in R. For creating a barplot in R you can use the base R barplot function.

How to create a simple barplot in Python?

To create a simple barplot, use the input vector and the name of each bar. In this example, T he height is a vector, or we can say our data, so the values determine the heights of the bars in the plot. In this bar chart, we have not mentioned any x-label, y-label, main title, color, and other properties. But we can define it.

What is Arg and Col in R barplot?

names.arg: It is a vector of names appearing under each bar. col: It is used to give colors to the bars in the graph. There are lots of other parameters, but you will mostly use these. Simple barplot Example in R

What is a multiple bar plot in statistics?

In multiple bar plots, we have various bar plots in the form of horizontal and vertical rectangular bars. In these multiple bar plots, the bar represents the relationship between numeric and categorical variables. Let’s learn to create a multiple bar plot with the help of the following examples.


Video Answer


3 Answers

Do not use special character + in colnames as already pointed by Bernhard: Here is a way where you could relabel the x axis of the plot by hand:

  1. Give adequate colnames in your matrix: see here comment of whuber: https://stats.stackexchange.com/questions/163280/naming-convention-for-column-names

  2. use xaxt = "n" in barplot to remove x labels

  3. use axis to insert x labels manually:

data <- as.matrix(data.frame("A" = c(18,7),
                             "Bplus" = c(3,2),
                             "B" = c(3,3),
                             "Cplus" = c(6,0),
                             "C" = c(7,0),
                             "D" = c(0,4),
                             "E" = c(5,23)))


barplot (data,
         col = c("red","blue"),
         beside = TRUE,
         xlab = "Grade",
         ylab = "Frequency",
         xaxt = "n")
         
axis(1, at = seq(2, 20, 3), labels = c("A", "B+", "B", "C+", "C", "D", "E"))


legend("topleft",
       c("IFC6503-A","IFC6510"),
       fill = c("red","blue"),
       inset = c(.01,0)
)

enter image description here

like image 58
TarJae Avatar answered Oct 18 '22 00:10

TarJae


The name of the columns in the data doesn't have B+ and C+

You can just rename the columns like this after loading the file:

colnames(data)[2] <-'B+'
colnames(data)[4] <-'C+'

Now plot with the same code

barplot (data,
         col = c("red","blue"),
         beside = TRUE,
         xlab = "Grade",
         ylab = "Frequency")


legend("topleft",
       c("IFC6503-A","IFC6510"),
       fill = c("red","blue"),
       inset = c(.01,0)
)

Output

like image 4
Shibaprasadb Avatar answered Oct 17 '22 22:10

Shibaprasadb


A tidyverse approach

library(tidyverse)

data %>% 
  pivot_longer(cols = everything()) %>% 
  group_by(name) %>% 
  mutate(
    name = str_replace(name,"\\.","+"),
    grp = row_number(),
    grp = if_else(grp == 1,"IFC6503-A","IFC6510")
    ) %>% 
  ggplot(aes(x = name, y = value,fill = grp))+
  geom_col(position = position_dodge())+
  scale_fill_manual(values = c("IFC6503-A" = "red","IFC6510" = "blue"))+
  labs(
    x = "Grade",
    y = "Frequency"
  )

enter image description here

like image 1
Vinícius Félix Avatar answered Oct 17 '22 23:10

Vinícius Félix