0% found this document useful (0 votes)
13 views2 pages

R Codes For Graphs

Uploaded by

abdulhananfg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

R Codes For Graphs

Uploaded by

abdulhananfg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

### Load ggplot2 library

####install.packages("package_name")
install.packages("ggplot2")

library(ggplot2)

# Data
data <- data.frame(
category = c("A", "B", "C", "D", "E"),
value = c(7, 12, 28, 3, 41)
)

# Create bar plot


ggplot(data, aes(x = category, y = value, fill = category)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Bar Chart using ggplot2", x = "Categories", y = "Values")

# Data
heights <- c(7, 12, 28, 3, 41)
labels <- c("A", "B", "C", "D", "E")

# Create bar plot


barplot(heights,
names.arg = labels,
col = "blue",
main = "Simple Bar Chart",
xlab = "Categories",
ylab = "Values")

#############multiple bar###########
Data
values <- matrix(c(7, 12, 28, 3, 41,
5, 8, 13, 17, 21),
nrow = 2, byrow = TRUE)

# Bar plot with grouping


barplot(values,
beside = TRUE,
col = c("blue", "red"),
names.arg = c("A", "B", "C", "D", "E"),
legend.text = c("Group 1", "Group 2"),
main = "Grouped Bar Chart",
xlab = "Categories",
ylab = "Values")

########histogram#########
# Sample data
data <- c(5, 12, 14, 15, 18, 21, 22, 22, 23, 24, 25, 26, 28, 30)

# Create a basic histogram


hist(data,
col = "skyblue",
main = "Basic Histogram",
xlab = "Values",
ylab = "Frequency",
border = "black")
#########boxplot##########
# Sample data
data <- c(12, 15, 13, 10, 8, 18, 20, 11, 14, 17)

# Create box plot


boxplot(data,
main = "Basic Box Plot",
ylab = "Values",
col = "lightblue",
border = "black")

You might also like