Change color of plot in R - r

I'm trying to find out why I'm not able to change the color of my bars. Hope you can help, I'm kinda new to this.
ggplot2(data, aes(x = data$Crop, y = data$"2018"))+
geom_bar(color="black", fill="red") +
theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)) +
main="Production value per crop in 2018" +
ylab("Production value in 2018")+
xlab("Crop")+
Hope to hear from you.

A little tweak of your code:
library(tidyverse)
data <- data.frame(Crop = c("East","West","North","South"),
Y2018 = c(1000,2000,3000,400),
stringsAsFactors = TRUE)
ggplot(data, aes(x = data$Crop, y = data$Y2018)) +
geom_col(color="black", fill="red") +
theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)) +
labs(
title = "Production value per crop in 2018",
ylab = "Production value in 2018",
xlab = "Crop"
)
Hope it helps

This is the code, which worked, though xlab gave an error referring to unexpected symbol
setwd("C:/Users/####/OneDrive/Documenten") # include the path to your data
data<-read_xlsx("datasetR.xlsx")
str(data) # get an overview of the data
library(ggplot2)
library(tidyverse)
dCrop <- data$Crop
d2018 <- data$"2018"
ggplot(data, aes(x = dCrop, y = d2018)) +
geom_col(width=1, fill = "red") +
theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)) +
ggtitle("Production value per crop in 2018")+
ylab("Production value in 2018") +
xlab("Crop")
enter image description here

Related

Having issues with bar chart x axis labels overlapping and spacing

I need to essentially do a graph of x-axis (the date) and y-axis (volume sold) and have each day in a calendar year be represented.
Issues - first is the white space between dates. I tried to use factor(Date), but ran into issues when I wanted additional changes to the graph. The other issue, is the x-axis is currently by month. This looks fine. However, when I try to do it by day, I get ...
In short, it looks like a mess. Probably because I'm trying to put every date in at once. Below is my code as is.
ggplot(MyDataCSV, aes(x = Date, y = Volume)) +
geom_col(stat = "identity",
width = 0.9,
fill = "coral",
alpha = 0.5,
colour = "black",
position = "dodge") +
scale_x_date(date_breaks = "1 day", labels = date_format("%m/%d")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ggtitle("Volume Sold by Date") +
theme(plot.title = element_text(hjust = 0.5))
I'm a beginner to R, so I know this is beginner level, but I'm very confused. I have ggplot, tidyverse, dplyr, lubridate, and scales installed. Essentially, I want my labels on the x-axis to look like the first picture, except with every date in my data set (about a year)
One solution is to specify the dimensions of the saved figure, e.g.
# Load libraries
library(tidyverse)
library(lubridate)
# Generate a fake dataset (minimal reproducible example)
df <- data.frame(Date = seq.Date(from = ymd("2021-01-01"),
to = ymd("2021-12-31"),
by = "1 day"),
Volume = runif(365, 0, 4e+08))
# Plot the fake data
ggplot(df, aes(x = Date, y = Volume)) +
geom_col(stat = "identity",
width = 0.9,
fill = "coral",
alpha = 0.5,
colour = "black",
position = "dodge") +
scale_x_date(date_breaks = "1 day", labels = scales::date_format("%m/%d")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ggtitle("Volume Sold by Date") +
theme(plot.title = element_text(hjust = 0.5))
# Save the plot
ggsave(filename = "example_1.png", width = 60, height = 10, limitsize = FALSE)
Then, if you zoom in, you can see the dates don't overlap:
Otherwise, you could change the date_breaks to "1 month", or "1 week" to stop the dates overlapping whilst keeping a 'normal' figure size:
# Plot the fake data
ggplot(df, aes(x = Date, y = Volume)) +
geom_col(stat = "identity",
width = 0.9,
fill = "coral",
alpha = 0.5,
colour = "black",
position = "dodge") +
scale_x_date(date_breaks = "1 week", labels = scales::date_format("%m/%d")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ggtitle("Volume Sold by Date") +
theme(plot.title = element_text(hjust = 0.5))
Or, the option that I would probably recommend, you could create facets by e.g. every 3 months:
# Load libraries
library(tidyverse)
library(lubridate)
# Generate a fake dataset (minimal reproducible example)
df <- data.frame(Date = seq.Date(from = ymd("2021-01-01"),
to = ymd("2021-12-31"),
by = "1 day"),
Volume = runif(365, 0, 4e+08))
plot_labels <- c(
"1" = "First Quarter, 2021",
"2" = "Second Quarter, 2021",
"3" = "Third Quarter, 2021",
"4" = "Fourth Quarter, 2021"
)
# Plot the fake data
df %>%
mutate(quarter = cut.Date(Date, breaks = "quarter", labels = FALSE)) %>%
ggplot(., aes(x = Date, y = Volume)) +
geom_col(stat = "identity",
width = 0.9,
fill = "coral",
alpha = 0.5,
colour = "black",
position = "dodge") +
scale_x_date(date_breaks = "1 day", labels = scales::date_format("%m/%d")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ggtitle("Volume Sold by Date") +
theme(plot.title = element_text(hjust = 0.5)) +
facet_wrap(~ quarter, ncol = 1, scales = "free_x",
labeller = labeller(quarter = plot_labels))

Adding a shaded area to facet

I am really new to R and trying to lean it myself but now I got stuck in a place. I have tried to add a shaded area to a facet but it adds a shaded area to every facet. I wanna highlight a time from 2017-08-08 to 2017-08-16 only and I don't know how to do it.
The code I have right now looks like this:
ggplot(data = Andmed3, aes(x= Aeg, y = arch, group = Prooviala)) +
stat_summary(geom = "errorbar",fun.data = mean_se, aes(group = Prooviala, color = Prooviala)) +
stat_summary(fun = mean,geom="line",lwd=1,aes (group= Prooviala, color = Prooviala)) +
stat_summary(geom = "point", fun = mean, shape = 16, size = 2, aes(color = Prooviala)) +
facet_grid(~Periood, scales = "free_x", space = "free_x") +
labs(x = "KuupƤev", y = "Arhede 16S rRNA geenikoopiate arv")+
theme_bw() +
theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
scale_x_datetime(date_breaks = "1 week", date_labels = " %e %b %y") +
geom_rect(aes(xmin = as.POSIXct('2017-08-08', format = '%Y-%m-%d'),xmax = as.POSIXct('2017-08-16', format = '%Y-%m-%d'),ymin = -Inf,ymax = Inf), alpha = 0.2, fill = 'grey')
I need my graph to look like my second picture but just with the highlighted background in the first facet.
Can someone help me with the code?
Thank You!

How to show date x-axis labels every 3 or 6 months in ggplot2

I generate a plot with code below:
ggplot(reshaped_median, aes(x= Month_Yr, y = value))+
geom_line(aes(color = Sentiments)) +
geom_point(aes(color = Sentiments)) +
labs(title = 'Change in Sentiments (in median)', x = 'Month_Yr', y = 'Proportion of Sentiments %') +
theme(axis.text.x = element_text(angle = 60, hjust = 1))
But as you can notice the dates labels in x-axis are too dense, so if I want to it displays date quarterly or on the half year (every 3 or 6 months).
The values from Month_Yr are with format %Y-%m.
How could I do that? Thanks.
First convert date by: df$Month_Yr <- as.Date(as.yearmon(df$Month_Yr))
Then use this can solve the issue:
ggplot(reshaped_median, aes(x= Month_Yr, y = value))+
geom_line(aes(color = Sentiments)) +
geom_point(aes(color = Sentiments)) +
#Here you set date_breaks ="6 month" or what you wish
scale_x_date(date_labels="%b-%d",date_breaks ="3 month")+
labs(title = 'Change in Sentiments (in median)', x = 'Month_Yr', y = 'Proportion of Sentiments %') +
theme(axis.text.x = element_text(angle = 60, hjust = 1))
Here's another way. With scale_x_date you can manipulate the breaks on your x-axis easily.
library(ggplot2)
library(tibble)
data <- tibble(
Month_Yr = seq.Date(from = as.Date("2010/01/01"), to = as.Date("2020/01/31"), by = "month"),
Value = runif(121, min = 0, max = 150)
)
p <- ggplot(data = data, aes(x = Month_Yr, y = Value)) +
geom_point() +
theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
scale_x_date(date_breaks = "6 months")
p

ggplot - x-axis shows data beyond specified range for longer time periods

Here's some sample data for a company's Net revenue split by two cohorts:
data <- data.frame(dates = rep(seq(as.Date("2000/1/1"), by = "month", length.out = 48), each = 2),
revenue = rep(seq(10000, by = 1000, length.out = 48), each = 2) * rnorm(96, mean = 1, sd = 0.1),
cohort = c("Group 1", "Group 2"))
I can show one year's worth of data and it returns what I would expect:
start = "2000-01-01"
end = "2000-12-01"
ggplot(data, aes(fill = cohort, x = dates, y = revenue)) +
geom_bar(stat = "identity", position = position_dodge(width = NULL)) +
xlab("Month") +
ylab("Net Revenue") +
geom_text(aes(label = round(revenue, 0)), vjust = -0.5, size = 3, position = position_dodge(width = 25)) +
scale_x_date(date_breaks = "1 month", limits = as.Date(c(start, end))) +
ggtitle("Monthly Revenue by Group") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 60, hjust = 1), plot.title = element_text(hjust = 0.5)) +
scale_fill_manual(values=c("#00BFC4", "#F8766D"))
But if I expand the date range to two years or more and rerun the graph, it shows additional months on both sides of the x-axis despite not displaying any information on the y-axis.
start = "2000-01-01"
end = "2001-12-01"
#rerun the ggplot code from above
Note the non-existant data points for 1999-12-01 and 2002-01-01. Why do these appear and how can I remove them?
Many (all?) of the scale_* functions take expand= as an argument. It's common in R plots (both base and ggplot2) to expand the axes just a little bit (4% on each end, I believe), I think so that none of the lines/points are scrunched up against the "box" boundary.
If you include expand=c(0,0), you get what you want.
(BTW: you have mismatched parens. Fixed here.)
ggplot(data, aes(fill = cohort, x = dates, y = revenue)) +
geom_bar(stat = "identity", position = position_dodge(width = NULL)) +
xlab("Month") +
ylab("Net Revenue") +
geom_text(aes(label = round(revenue, 0)), vjust = -0.5, size = 3, position = position_dodge(width = 25)) +
scale_x_date(date_breaks = "1 month", limits = as.Date(c(start, end)), expand = c(0, 0)) +
ggtitle("Monthly Revenue by Group") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 60, hjust = 1), plot.title = element_text(hjust = 0.5)) +
scale_fill_manual(values=c("#00BFC4", "#F8766D"))
I am not sure what exactly the issue is but if you change from "Date" class on x-axis to any other it seems to work as expected. Also filtering the data for the specific range before passing it to ggplot.
For example in this case changing dates to month-year format,
library(dplyr)
library(ggplot2)
start = as.Date("2000-01-01")
end = as.Date("2001-12-01")
all_fac <- c(outer(month.abb, 2000:2001, paste, sep = "-"))
data %>%
filter(between(dates, start, end)) %>%
mutate(dates = factor(format(dates, "%b-%Y"),levels = all_fac)) %>%
ggplot() + aes(fill = cohort, x = dates, y = revenue) +
geom_bar(stat = "identity", position = "dodge") +
xlab("Month") +
ylab("Net Revenue") +
geom_text(aes(label = round(revenue, 0))) +
ggtitle("Monthly Revenue by Group") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 60, hjust = 1), plot.title =
element_text(hjust = 0.5)) +
scale_fill_manual(values=c("#00BFC4", "#F8766D"))
Please beautify/change the labels on the bars.

How to fix ggplot double y axis in r

I am trying to make a graph with two y axis. I know there are a lot of other questions out there similar to this but I just cant seem to figure it out based on other posts
So the issue I am having is the y axis scale. Here is what I am doing
Time <- c("June-2018-30", "July-2018-31", "August-2018-31", "September-2018-30",
"October-2018-31", "November-2018-30", "December-2018-31", "January-2019-31",
"February-2019-28", "March-2019-31", "April-2019-30", "May-2019-31")
Bitcoin <- c(3.469861e-17, 3.188903e-17, 2.685114e-17, 2.42335e-17, 2.322641e-17,
2.447058e-17, 3.18029e-17, 2.944836e-17, 2.839419e-17, 2.76008e-17,
2.661607e-17, 2.536966e-17)
`USD Return` <- c(2.35e-13, 2.27e-13, 1.80e-13, 1.60e-13, 1.51e-13, 1.33e-13, 1.18e-13,
1.08e-13, 1.047e-13, 1.09e-13, 1.37e-13, 1.83e-13)
total.values3 <- data.frame(Time, Bitcoin,`USD Return`, stringsAsFactors = F)
library(ggplot2)
ggplot(data=total.values3, aes(x=Time, y=`USD Return`, group=1)) +
geom_line(aes(y = `USD Return`), color = "blue") +
geom_line(aes(y = Bitcoin), color = "red") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous("USD Return",
sec.axis = sec_axis(~./10000, name = "Bitcoin Return")) +
scale_x_date(labels=date_format("%B-%Y-%d"),
date_labels = "%B-%Y", breaks = total.values3$Time)
Here is a picture of what output
I am not sure what is going wrong. I can see that the scale is wrong. I can't figure out why the bitcoin line is just a straight line. I also don't know why the y axis on the right side goes into the negative
total.values3$Time <- as.Date(total.values3$Time, format = "%B-%Y-%d")
ggplot(data=total.values3, aes(x=Time, group=1)) +
geom_line(aes(y = `USD Return`), color = "blue") +
geom_line(aes(y = Bitcoin*10000), color = "red") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous("USD Return",
sec.axis = sec_axis(~./10000, name = "Bitcoin Return")) +
scale_x_date(labels=date_format("%B-%Y-%d"),
date_labels = "%B-%Y", breaks = total.values3$Time)
This should do the trick.

Resources