I have the following bar plot created using R ggplot. How do I dynamically update the distances between the bars on the plot using the 'distance' column of the same data frame.
library(tidyverse)
data.frame(name = c("A","B","C","D","E"),
value = c(34,45,23,45,75),
distance = c(3,4,1,2,5)) %>%
ggplot(aes(x = name, y = value)) +
geom_col()
Related
My data consists of a date variable and four numeric variables, of the 4 numeric variables I wish to plot two of these as a stacked bar chart and the remaining variables as line charts.
Is it possible to create two line charts and a stacked bar chart in a single plot using ggplot?
My data looks as follows:
data <- tibble(Month = 1:12,Brands = c(1,1,1,1,1,1,2,2,2,2,2,2),Generics = Brands + 1,Metric1 = c(5,5,5,5,5,5,6,6,7,8,9,10),Metric2 = c(10,10,11,11,12,13,14,15,16,17,18,19))
I wish to plot months on the x axis, Brands1 & Brands2 as stacked bar charts and Metric1 & Metric2 as line charts all on the same chart if possible.
Something like this?
library(tidyverse)
data <- tibble(Month = 1:12,Brands = c(1,1,1,1,1,1,2,2,2,2,2,2),Generics = Brands + 1,Metric1 = c(5,5,5,5,5,5,6,6,7,8,9,10),Metric2 = c(10,10,11,11,12,13,14,15,16,17,18,19))
data %>%
pivot_longer(cols = c(Brands,Generics)) %>%
pivot_longer(cols = c(Metric1,Metric2),
names_to = "metric_name",values_to = "metric_value") %>%
ggplot(aes(Month))+
geom_col(aes(y = value, fill = name))+
geom_line(aes(y = metric_value, col = metric_name),size = 1.25)+
scale_x_continuous(breaks = 1:12)+
scale_color_manual(values = c("black","purple"))
I'm new to R and am just trying to graph some plots using the ggplot2 function that I just learned with the palmer penguins dataset. I want to create a simple bar graph with species at the x-axis and average body mass at the y-axis. I begin by grouping the penguins by species and calculating the average as follow:
avg_body_mass <- penguins %>%
drop_na(body_mass_g) %>%
group_by(species) %>%
summarize(mean(body_mass_g))
This results in a new data frame that looks like this:
[1]: https://i.stack.imgur.com/Xz24r.png
The first problem is: How do I change the name of the average body mass column so it is not called "mean(body_mass_g)"?
The following is my code for the graph:
ggplot(data = penguins) + geom_bar(mapping = aes(x = species, y = avg_body_mass))
Alternatively, I also tried this:
ggplot(data = avg_body_mass) + geom_bar(mapping = aes(x = species, y = mean(body_mass_g)))
I don't think I should put a mean function in the geom_bar, but that's the name of the column and I don't know how to change it.
so I'm trying to Plot chart. I filtered the original dataset Datengf to get the median income of each year (MULTYEAR) and the variable Schulbildung. No chart looks like this: chart. Now I want to plot chart by using ggplot and geom_line. On the x-axis MULTYEAR and on the y-axis the medianincome. But I want it to be a different line and color for each value of Schulbildung.
Chart code:
chart <- Datengf %>%
filter(SEX == 1)%>%
group_by(MULTYEAR,Schulbildung) %>%
summarise(medianincome = median(INCWAGE))%>%
ungroup()%>%
mutate(Schulbildung = ifelse(Schulbildung < 12, "others", Schulbildung)) %>%
group_by(Schulbildung,MULTYEAR)%>%
summarise(medianincome = sum(medianincome))
I tried using
chartplot <- chart %>%
ggplot(aes(x = MULTYEAR, y = medianincome))+
geom_line()
but the chart is an complete mess.
Specify color in the aes function:
chartplot <- chart %>%
ggplot(aes(x = MULTYEAR, y = medianincome, color = Schulbildung))+
geom_line()
I have the following data:
CT VT TT
A* 5.923076923 6.529411765 5.305555556
Not A* 5.555555556 6.434782609 5.352941176
I want to make a grouped bar chart in R from the data such that the grouping is on A* and Not A*, the x-axis ticks are CT, VT and TT and the numeric values are plotted in the y-direction.
What do I need to do to produce the bar plot from this raw .csv data?
Next time, you should provide a reproducible example, but I use ggplot2 to create the desired bar plot:
Before jumping into the main body, make sure you have the required packages installed as follows:
install.packages(c("ggplot2","data.table"))
Now for a stacked bar chart:
require(ggplot2)
require(data.table)
data <- data.frame(CT = c( 5.923076923 ,5.555555556),
VT = c(6.529411765,6.434782609),
TT = c(5.305555556, 5.352941176))
rownames(data) <- c("A*", "Not A*")
long_format <- melt(as.matrix(data))
ggplot(long_format, aes(x = Var2,
y = value,
fill = Var1)) +
geom_col()
A grouped bar chart:
ggplot(data = long_format,
aes(x = Var2,
y = value,
fill = Var1)) +
geom_bar(position = "dodge",
stat = "identity")
i need your help.
I was trying to do a stacked bar plot in R and i m not succeding for the moment. I have read several post but, no succed neither.
Like i am newbie, this is the chart I want (I made it in excel)
And this is how i have the data
Thank you in advance
I would use the package ggplot2 to create this plot as it is easier to position text labels than compared to the basic graphics package:
# First we create a dataframe using the data taken from your excel sheet:
myData <- data.frame(
Q_students = c(1000,1100),
Students_with_activity = c(950, 10000),
Average_debt_per_student = c(800, 850),
Week = c(1,2))
# The data in the dataframe above is in 'wide' format, to use ggplot
# we need to use the tidyr package to convert it to 'long' format.
library(tidyr)
myData <- gather(myData,
Condition,
Value,
Q_students:Average_debt_per_student)
# To add the text labels we calculate the midpoint of each bar and
# add this as a column to our dataframe using the package dplyr:
library(dplyr)
myData <- group_by(myData,Week) %>%
mutate(pos = cumsum(Value) - (0.5 * Value))
#We pass the dataframe to ggplot2 and then add the text labels using the positions which
#we calculated above to place the labels correctly halfway down each
#column using geom_text.
library(ggplot2)
# plot bars and add text
p <- ggplot(myData, aes(x = Week, y = Value)) +
geom_bar(aes(fill = Condition),stat="identity") +
geom_text(aes(label = Value, y = pos), size = 3)
#Add title
p <- p + ggtitle("My Plot")
#Plot p
p
so <- data.frame ( week1= c(1000,950,800), week2=c(1100,10000,850),row.names = c("Q students","students with Activity","average debt per student")
barplot(as.matrix(so))