Program 1
# Create the data for the chartA <- c(17, 32, 8, 53, 1)# Plot the bar chartbarplot(A, horiz = TRUE, xlab = "X-axis", ylab = "Y-axis", main ="Horizontal Bar Chart" )
How to execute:
Select total programRight clickClick on Run (or) Ctrl+Enter
program 2
# Create the data for the chartA <- c(17, 2, 8, 13, 1, 22)B <- c("Jan", "feb", "Mar", "Apr", "May", "Jun") # Plot the bar chartbarplot(A, names.arg = B, xlab ="Month", ylab ="Articles", col ="green", main ="GeeksforGeeks-Article chart")
Program 3
# Create the data for the chart
A <- c(17, 2, 8, 13, 1, 22)
B <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
# Plot the bar chart with text features
barplot(A, names.arg = B, xlab = "Month",
ylab = "Articles", col = "steelblue",
main = "GeeksforGeeks - Article Chart",
cex.main = 1.5, cex.lab = 1.2, cex.axis = 1.1)
# Add data labels on top of each bar
text(
x = barplot(A, names.arg = B, col = "steelblue", ylim = c(0, max(A) * 1.2)),
y = A + 1, labels = A, pos = 3, cex = 1.2, col = "black"
)
Program 4
colors = c("green", "orange", "brown")months <- c("Mar", "Apr", "May", "Jun", "Jul")regions <- c("East", "West", "North") # Create the matrix of the values.Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11), nrow = 3, ncol = 5, byrow = TRUE) # Create the bar chartbarplot(Values, main = "Total Revenue", names.arg = months, xlab = "Month", ylab = "Revenue", col = colors, beside = TRUE) # Add the legend to the chartlegend("topleft", regions, cex = 0.7, fill = colors)
Program 5
colors = c("green", "orange", "brown")months <- c("Mar", "Apr", "May", "Jun", "Jul")regions <- c("East", "West", "North") # Create the matrix of the values.Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11), nrow = 3, ncol = 5, byrow = TRUE) # Create the bar chartbarplot(Values, main = "Total Revenue", names.arg = months, xlab = "Month", ylab = "Revenue", col = colors, beside = TRUE) # Add the legend to the chartlegend("topleft", regions, cex = 0.7, fill = colors)
program 6
colors = c("green", "orange", "brown")months <- c("Mar", "Apr", "May", "Jun", "Jul")regions <- c("East", "West", "North") # Create the matrix of the values.Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11), nrow = 3, ncol = 5, byrow = TRUE) # Create the bar chartbarplot(Values, main = "Total Revenue", names.arg = months, xlab = "Month", ylab = "Revenue", col = colors) # Add the legend to the chartlegend("topleft", regions, cex = 0.7, fill = colors)
Program 7
3D Graph
# To illustrate simple right circular cone
cone <- function(x, y){
sqrt(x ^ 2 + y ^ 2)
}
# prepare variables.
x <- y <- seq(-1, 1, length = 30)
z <- outer(x, y, cone)
# plot the 3D surface
persp(x, y, z)
program 8
# Adding Titles and Labeling Axes to Plot
cone <- function(x, y){
sqrt(x ^ 2 + y ^ 2)
}
# prepare variables.
x <- y <- seq(-1, 1, length = 30)
z <- outer(x, y, cone)
# plot the 3D surface
# Adding Titles and Labeling Axes to Plot
persp(x, y, z,
main="Perspective Plot of a Cone",
zlab = "Height",
theta = 30, phi = 15,
col = "orange", shade = 0.4)
Program 9
# Visualizing a simple DEM model
z <- 2 * volcano # Exaggerate the reliefx <- 10 * (1:nrow(z)) # 10 meter spacing (S to N)y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W)
# Don't draw the grid lines : border = NApar(bg = "gray")persp(x, y, z, theta = 135, phi = 30, col = "brown", scale = FALSE, ltheta = -120, shade = 0.75, border = NA, box = FALSE)
Program 10
# Generate data for the plotx <- seq(-5, 5, length.out = 50)y <- seq(-5, 5, length.out = 50)z <- outer(x, y, function(x, y) sin(sqrt(x^2 + y^2))/sqrt(x^2 + y^2))
# Create the plotpersp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")
Program 11
library(plotly)
df = read.csv('https://raw.githubusercontent.com/plotly/datasets/master/streamtube-wind.csv')
fig <- df %>% plot_ly( type = 'streamtube', x = ~x, y = ~y, z = ~z, u = ~u, v = ~v, w = ~w, starts = list( x = rep(80, 16), y = rep(c(20,30,40,50), 4), z = c(rep(0,4),rep(5,4),rep(10,4),rep(15,4)) ), sizeref = 0.3, showscale = F, maxdisplayed = 3000 ) fig <- fig %>% layout( scene = list( aspectratio = list( x = 2, y = 1, z = 0.3 ) ), margin = list( t = 20, b = 20, l = 20, r = 20 ) )
fig
Program 12
library(plotly)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/clebsch-cubic.csv')
fig <- plot_ly(
df,
type='isosurface',
x = ~x,
y = ~y,
z = ~z,
value = ~value,
isomin = -10,
isomax = 10,
surface = list(show = TRUE, count = 4, fill = 0.8, pattern = 'odd'),
caps = list(
x = list(show = TRUE),
y = list(show = TRUE),
z = list(show = TRUE)
)
)
fig <- fig %>%
layout(
margin=list(t = 0, l = 0, b = 0),
scene=list(
camera=list(
eye=list(
x = 1.86,
y = 0.61,
z = 0.98
)
)
)
)
fig
Program 13
# Library
library(leaflet)
# load example data (Fiji Earthquakes) + keep only 100 first lines
data(quakes)
quakes = head(quakes, 100)
# Create a color palette with handmade bins.
mybins=seq(4, 6.5, by=0.5)
mypalette = colorBin( palette="YlOrBr", domain=quakes$mag, na.color="transparent", bins=mybins)
# Final Map
leaflet(quakes) %>%
addTiles() %>%
setView( lat=-27, lng=170 , zoom=4) %>%
addProviderTiles("Esri.WorldImagery") %>%
addCircleMarkers(~long, ~lat,
fillColor = ~mypalette(mag), fillOpacity = 0.7, color="white", radius=8, stroke=FALSE
) %>%
addLegend( pal=mypalette, values=~mag, opacity=0.9, title = "Magnitude", position = "bottomright" )
Program 14
# Libraries
library(babynames)
# Load dataset from github
data <- babynames %>%
filter(name %in% c("Ashley", "Amanda", "Jessica", "Patricia", "Linda", "Deborah", "Dorothy", "Betty", "Helen")) %>%
filter(sex=="F")
# line plot = spaghetti chart
data %>%
ggplot( aes(x=year, y=n, group=name, color=name)) +
geom_line() +
ggtitle("Popularity of American names in the previous 30 years")
Program 15
1. Data visualization with dataset
# Horizontal Bar Plot for
# Ozone concentration in air
barplot(airquality$Ozone,
main = 'Ozone Concenteration in air',
xlab = 'ozone levels', horiz = TRUE)
2. Data visualization with dataset
# Vertical Bar Plot for
# Ozone concentration in air
barplot(airquality$Ozone, main = 'Ozone Concenteration in air',
xlab = 'ozone levels', col ='blue', horiz = FALSE)
3. Data visualization with dataset
# Histogram for Maximum Daily Temperature
data(airquality)
hist(airquality$Temp, main ="La Guardia Airport's\
Maximum Temperature(Daily)",
xlab ="Temperature(Fahrenheit)",
xlim = c(50, 125), col ="yellow",
freq = TRUE)
4. Data visualization with dataset
# Box plot for average wind speed
data(airquality)
boxplot(airquality$Wind, main = "Average wind speed\
at La Guardia Airport",
xlab = "Miles per hour", ylab = "Wind",
col = "orange", border = "brown",
horizontal = TRUE, notch = TRUE)
5. Data visualization with dataset
# Multiple Box plots, each representing
# an Air Quality Parameter
boxplot(airquality[, 0:4],
main ='Box Plots for Air Quality Parameters')
6. Data visualization with dataset
# Scatter plot for Ozone Concentration per month
data(airquality)
plot(airquality$Ozone, airquality$Month,
main ="Scatterplot Example",
xlab ="Ozone Concentration in parts per billion",
ylab =" Month of observation ", pch = 19)
7. Data visualization with dataset
# Set seed for reproducibility
# set.seed(110)
# Create example data
data <- matrix(rnorm(50, 0, 5), nrow = 5, ncol = 5)
# Column names
colnames(data) <- paste0("col", 1:5)
rownames(data) <- paste0("row", 1:5)
# Draw a heatmap
heatmap(data)
Program 16
# library
library(tidyverse)
# Create dataset
data <- data.frame(
individual=paste( "Mister ", seq(1,60), sep=""),
value=sample( seq(10,100), 60, replace=T)
)
# Set a number of 'empty bar'
empty_bar <- 10
# Add lines to the initial dataset
to_add <- matrix(NA, empty_bar, ncol(data))
colnames(to_add) <- colnames(data)
data <- rbind(data, to_add)
data$id <- seq(1, nrow(data))
# Get the name and the y position of each label
label_data <- data
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)
# Make the plot
p <- ggplot(data, aes(x=as.factor(id), y=value)) + # Note that id is a factor. If x is numeric, there is some space between the first bar
geom_bar(stat="identity", fill=alpha("green", 0.3)) +
ylim(-100,120) +
theme_minimal() +
theme(
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
coord_polar(start = 0) +
geom_text(data=label_data, aes(x=id, y=value+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE )
p
Program 17
# Libraries
library(ggplot2)
library(dplyr)
library(hrbrthemes)
library(viridis)
# create a dataset
data <- data.frame(
name=c( rep("A",500), rep("B",500), rep("B",500), rep("C",20), rep('D', 100) ),
value=c( rnorm(500, 10, 5), rnorm(500, 13, 1), rnorm(500, 18, 1), rnorm(20, 25, 4), rnorm(100, 12, 1) )
)
# sample size
sample_size = data %>% group_by(name) %>% summarize(num=n())
# Plot
data %>%
left_join(sample_size) %>%
mutate(myaxis = paste0(name, "\n", "n=", num)) %>%
ggplot( aes(x=myaxis, y=value, fill=name)) +
geom_violin(width=1.4) +
geom_boxplot(width=0.1, color="grey", alpha=0.2) +
scale_fill_viridis(discrete = TRUE) +
theme_ipsum() +
theme(
legend.position="none",
plot.title = element_text(size=11)
) +
ggtitle("A Violin wrapping a boxplot") +
xlab("")
Program 18
# Libraries
library(tidyverse)
library(hrbrthemes)
library(babynames)
library(viridis)
# Load dataset from github
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv", header=T)
data$date <- as.Date(data$date)
# Load dataset from github
don <- babynames %>%
filter(name %in% c("Ashley", "Amanda", "Mary", "Deborah", "Dorothy", "Betty", "Helen", "Jennifer", "Shirley")) %>%
filter(sex=="F")
# Plot
don %>%
ggplot( aes(x=year, y=n, group=name, fill=name)) +
geom_area() +
scale_fill_viridis(discrete = TRUE) +
theme(legend.position="none") +
ggtitle("Popularity of American names in the previous 30 years") +
theme_ipsum() +
theme(
legend.position="none",
panel.spacing = unit(0, "lines"),
strip.text.x = element_text(size = 8),
plot.title = element_text(size=13)
) +
facet_wrap(~name, scale="free_y")
program 19 (Animation)
# Get data:
library(gapminder)
# Charge libraries:
library(ggplot2)
library(gganimate)
# Make a ggplot, but add frame=year: one image per year
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
geom_point() +
scale_x_log10() +
theme_bw() +
# gganimate specific bits:
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
# Save at gif:
anim_save("271-ggplot2-animated-gif-chart-with-gganimate1.gif")
Program 20
# libraries:
library(ggplot2)
library(gganimate)
# Make 2 basic states and concatenate them:
a <- data.frame(group=c("A","B","C"), values=c(3,2,4), frame=rep('a',3))
b <- data.frame(group=c("A","B","C"), values=c(5,3,7), frame=rep('b',3))
data <- rbind(a,b)
# Basic barplot:
ggplot(a, aes(x=group, y=values, fill=group)) +
geom_bar(stat='identity')
# Make a ggplot, but add frame=year: one image per year
ggplot(data, aes(x=group, y=values, fill=group)) +
geom_bar(stat='identity') +
theme_bw() +
# gganimate specific bits:
transition_states(
frame,
transition_length = 2,
state_length = 1
) +
ease_aes('sine-in-out')
# Save at gif:
anim_save("288-animated-barplot-transition.gif")
Program 21
1. Barplot
# Horizontal Bar Plot for
# Ozone concentration in air
barplot(weatherHistory$Humidity,
main = 'Humidity in air',
xlab = 'Humidity levels', horiz = TRUE)
2. Histogram
# Horizontal Bar Plot for # Ozone concentration in air hist(weatherHistory$Humidity, main = 'Humidity Concenteration in air', xlab = 'Humidity levels', horiz = TRUE)
3. Box plot
# Box plot for average wind speed data(weatherHistory)
boxplot(weatherHistory$`Temperature (C)`, main = "Average Temp", xlab = "in a day", ylab = "windspeed", col = "orange", border = "brown", horizontal = TRUE, notch = TRUE)
4. Plot
# Scatter plot for Ozone Concentration per month data(weatherHistory)
plot(weatherHistory$`Wind Speed (km/h)`, weatherHistory$`Wind Bearing (degrees)`, main ="Scatterplot Example", xlab ="Ozone Concentration in parts per billion", ylab =" Month of observation ", pch = 19)
Program 22
# Install package if not installedif (!require(plotly)) { install.packages("plotly") library(plotly)} else { library(plotly)}
############################## 1️⃣ 3D SCATTER PLOT#############################
# Generate sample dataset.seed(123)df <- data.frame( x = rnorm(200), y = rnorm(200))
df$z <- df$x^2 + df$y^2
# Create interactive 3D scatter plotscatter_plot <- plot_ly( df, x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "markers", marker = list( size = 4, color = ~z, colorscale = "Viridis" )) %>% layout(title = "3D Scatter Plot")
scatter_plot
############################## 2️⃣ 3D SURFACE PLOT#############################
# Create grid datax <- seq(-10, 10, length.out = 50)y <- seq(-10, 10, length.out = 50)
z <- outer(x, y, function(x, y) { sin(sqrt(x^2 + y^2))})
# Create interactive 3D surface plotsurface_plot <- plot_ly( x = x, y = y, z = z, type = "surface") %>% layout(title = "3D Surface Plot")
surface_plot

Wonderfull Blog Article.Thanks for sharing information.
ReplyDeleteInformatica Cloud Training in Hyderabad
Informatica Online Training
Informatica Training Online
Informatica Training in Ameerpet
Informatica Cloud Training
Informatica Cloud Online Training
IICS Training in Hyderabad
Informatica Training in Hyderabad