plotting line graph with two lines in R - r

I'm trying to create a simple double line graph with a dataset I made. Here's the data:
date <- c("2021-04-06","2021-04-10", "2021-04-14", "2021-04-18")
as.Date(date)
graded <- c(3408, 3572, 3647, 3864)
psa10 <- c(2099, 2130, 2147, 2193)
graded_marvel <- data.frame(date, graded, psa10)
graded_marvel
And here's what I did to try and graph this
library("ggplot2")
graph <- ggplot(graded_marvel, aes(date)) +
geom_line(aes(y = graded), color = "darkred") +
geom_line(aes(y = psa10), color = "blue")
print(graph)
All I get is an empty graph that has the correct values on the axes, but the graph just comes up empty. Not sure what to do. Any help is appreciated!

This happens because your date variable is not a date, so ggplot2 interprets is as a character and assigns a discrete x scale. This auto-groups your data based on the x-axis value, so every 'group' only has one observations, with which you cannot draw a line. The way to fix this is to convert your date to a proper Date class.
library(ggplot2)
date <- c("2021-04-06","2021-04-10", "2021-04-14", "2021-04-18")
graded <- c(3408, 3572, 3647, 3864)
psa10 <- c(2099, 2130, 2147, 2193)
graded_marvel <- data.frame(date, graded, psa10)
ggplot(graded_marvel, aes(as.Date(date))) +
geom_line(aes(y = graded), color = "darkred") +
geom_line(aes(y = psa10), color = "blue")
Created on 2021-04-19 by the reprex package (v1.0.0)

First get long format with pivot_longer.
Then plot with ggplot2.
library("ggplot2")
ggplot(df, aes(x=factor(date), y = values, group = names)) +
geom_point(aes(color=names)) +
geom_line(aes(linetype=names, color=names)) +
scale_colour_manual(values=c("darkred", "blue"))
data:
df <- graded_marvel %>%
pivot_longer(
cols = -date,
names_to = "names",
values_to = "values"
)

Related

geom_vline for values over a threshold on Y-axis

I have a ggplot of temperature values plotted against time. I'd like to add vertical lines to my graph where temperature exceeds a threshold (let's say 12 degrees).
reprex:
#example data
Temp <- c(10.55, 11.02, 6.75, 12.55, 15.5)
Date <- c("01/01/2000", "02/01/2000", "03/01/2000", "04/01/2000", "05/01/2000")
#data.frame
df1 <- data.frame(Temp, Date)
#plot
df1%>%
ggplot(aes(Date, format(as.numeric(Temp))))+
geom_line(group=1)
I thought I could maybe do something with geom_hline and then rotate 90 degrees. I went about this by trying to create an object of all values (to 2dp) between 12 and 20. I would then tell geom_hline to use that object to match values and draw the lines.
Then I get a bit stuck. I don't really know how to rotate the lines or whether that's even a good idea.
Disclaimer: I know my dates are not actually dates in the reprex, but they are in my rle.
geom_vline can accept an xintercept either
in the xintercept parameter (if you want to specify it manually) or
in aes(xintercept = ...) if you want to use values from a data frame. We can use data = . %>% filter... to use the same data frame that came into ggplot, but apply some further manipulations.
df1 %>%
mutate(Date = as.Date(Date, "%m/%d/%Y")) %>%
ggplot(aes(Date, Temp)) +
geom_line() +
geom_vline(data = . %>% filter(Temp > 12),
aes(xintercept = Date))
If you want to have vertical lines starting from the level of 12:
ggplot(df1, aes(Date, as.numeric(Temp)))+
geom_line(group=1) +
geom_segment(data= df1[df1$Temp>12,],
aes(x = Date,
xend = Date,
y = 12,
yend = Temp),
color = "blue", lwd = 1)

How to insert color code for two geom_step functions in the same grid

I am currently working in a comparison between two inventory levels and I want to plot two step graphs in the same grid with a color code. This is my code.
Intento1<-data.frame(Fecha, NivelI)
Intento2<-data.frame(Fecha, Nivel2)
#Printing the step graphs in one grid
ggplot()+geom_step(Intento1, mapping=aes(x=Fecha, y=NivelI))+geom_step(Intento2, mapping=aes(x=Fecha, y=Nivel2))
And it works fine plotting both graphs in the same grid, I could also add a different color to each graph but I couldnĀ“t add the little colored labels that appear normally at the right. All support is appreciated.
For example data dummy,
dummy <- data.table(
Fecha = seq(as.Date("2020/1/1"), as.Date("2020/1/31"), "day")
)
dummy$NivelI = runif(31, 0, 10)
dummy$Nivel2 = runif(31, 0, 10)
plot using reshape2::melt like below will work.
dummy %>%
melt(id.vars = "Fecha") %>%
ggplot(aes(Fecha, value, group = variable, color = variable)) +
geom_step() + guides(color = guide_legend(title = "aaa"))
In your case, to make dummy formed data, if Fecha, NivelI and Nivel2 are vectors, just try
df <- data.frame(
Fecha,
NivelI,
Nivel2
)
then
df %>%
melt(id.vars = "Fecha") %>%
ggplot(aes(Fecha, value, group = variable, color = variable)) +
geom_step() + guides(color = guide_legend(title = "aaa"))
where "aaa" will be your legend name.

Two ggplot with subset in pipe

I would like to plot two lines in one plot (both has the same axis), but one of the line is subset values from data frame.
I tries this
DF%>% ggplot(subset(., Cars == "A"), aes(Dates, sold_A)) +geom_line()+ ggplot(., (Dates, sold_ALL))
but this error occurred
object '.' not found
(1) You can't add a ggplot object to a ggplot object:
(2) Try taking the subset out of the call to ggplot.
DF %>%
subset(Cars == "A") %>%
ggplot(aes(Dates, sold_A)) +
geom_line() +
geom_line(data = DF, aes(Dates, sold_ALL))
I think you are misunderstanding how ggplot works. If we are attempting to do it your way, we could do:
DF %>% {ggplot(subset(., Cars == "A"), aes(Dates, sold_A)) +
geom_line(colour = "red") +
geom_line(data = subset(., Cars == "B"), colour = "blue") +
lims(y = c(0, 60))}
But it would be easier and better to map the variable Cars to the colour aesthetic, so your plot would be as simple as:
DF %>% ggplot(aes(Dates, sold_A, color = Cars)) + geom_line() + lims(y = c(0, 60))
Note that as well as being simpler code, we get the legend for free.
Data
Obviously, we didn't have your data for this question, but here is a constructed data set with the same name and same column variables:
set.seed(1)
Dates <- rep(seq(as.Date("2020-01-01"), by = "day", length = 20), 2)
Cars <- rep(c("A", "B"), each = 20)
sold_A <- rpois(40, rep(c(20, 40), each = 20))
DF <- data.frame(Dates, Cars, sold_A)
If you want only one plot, you would need to remove ggplot(., aes(Dates, sold_ALL)) and wrap directly into a structure like geom_line(data=., aes(Dates, sold_ALL)). Then, use the sage advice from #MrFlick. Here an example using iris data:
library(ggplot2)
library(dplyr)
#Example
iris %>%
{ggplot(subset(., Species == "setosa"), aes(Sepal.Length, Sepal.Width)) +
geom_point()+
geom_point(data=.,aes(Petal.Length, Petal.Width),color='blue')}
Output:
The ggplot(., aes(Dates, sold_ALL)) is creating a new canvas and the new plot.

plotly and ggplot legend order interaction

I have multiple graphs that I am plotting with ggplot and then sending to plotly. I set the legend order based the most recent date, so that one can easily interpret the graphs. Everything works great in generating the ggplot, but once I send it through ggplotly() the legend order reverts to the original factor level. I tried resetting the factors but this creates a new problem - the colors are different in each graph.
Here's the code:
Data:
Country <- c("CHN","IND","INS","PAK","USA")
a <- data.frame("Country" = Country,"Pop" = c(1400,1300,267,233,330),Year=rep(2020,5))
b <- data.frame("Country" = Country,"Pop" = c(1270,1000,215,152,280),Year=rep(2000,5))
c <- data.frame("Country" = Country,"Pop" = c(1100,815,175,107,250),Year=rep(1990,5))
Data <- bind_rows(a,b,c)
Legend Ordering Vector - This uses 2020 as the year to determine order.
Legend_Order <- Data %>%
filter(Year==max(Year)) %>%
arrange(desc(Pop)) %>%
select(Country) %>%
unlist() %>%
as.vector()
Then I create my plot and use Legend Order as breaks
Graph <- Data %>%
ggplot() +
geom_line(aes(x = Year, y = Pop, group = Country, color = Country), size = 1.2) +
scale_color_discrete(name = 'Country', breaks = Legend_Order)
Graph
But then when I pass this on to:
ggplotly(Graph)
For some reason plotly ignores the breaks argument and uses the original factor levels.
If I set the factor levels beforehand, the color schemes changes (since the factors are in a different order).
How can I keep the color scheme from graph to graph, but change the legend order when using plotly?
Simply recode your Conutry var as factor with the levels set according to Legend_Order. Try this:
library(plotly)
library(dplyr)
Country <- c("CHN","IND","INS","PAK","USA")
a <- data.frame("Country" = Country,"Pop" = c(1400,1300,267,233,330),Year=rep(2020,5))
b <- data.frame("Country" = Country,"Pop" = c(1270,1000,215,152,280),Year=rep(2000,5))
c <- data.frame("Country" = Country,"Pop" = c(1100,815,175,107,250),Year=rep(1990,5))
Data <- bind_rows(a,b,c)
Legend_Order <- Data %>%
filter(Year==max(Year)) %>%
arrange(desc(Pop)) %>%
select(Country) %>%
unlist() %>%
as.vector()
Data$Country <- factor(Data$Country, levels = Legend_Order)
Graph <- Data %>%
ggplot() +
geom_line(aes(x = Year, y = Pop, group = Country, color = Country), size = 1.2)
ggplotly(Graph)
To "lock in" the color assignment you can make use of a named color vector like so (for short I only show the ggplots):
# Fix the color assignments using a named color vector which can be assigned via scale_color_manual
cols <- scales::hue_pal()(5) # Default ggplot2 colors
cols <- setNames(cols, Legend_Order) # Set names according to legend order
# Plot with unordered Countries but "ordered" color assignment
Data %>%
ggplot() +
geom_line(aes(x = Year, y = Pop, color = Country), size = 1.2) +
scale_color_manual(values = cols)
# Plot with ordered factor
Data$Country <- factor(Data$Country, levels = Legend_Order)
Data %>%
ggplot() +
geom_line(aes(x = Year, y = Pop, color = Country), size = 1.2) +
scale_color_manual(values = cols)

Make different plots for each column in dataframe in one window r

I make for each variable in my dataframe a histogram, lineplot and boxplot to assess the distribution of each variable and plot these graphs in one window.
For variable VARIABLE my code looks like:
variable_name_string = "VARIABLE"
hist = qplot(VARIABLE, data = full_data_noNO, geom="histogram",
fill=I("lightblue"))+
theme_light()
avg_price = full_data_noNO %>%
group_by(Month, Country) %>%
dplyr::summarize(avg = mean(VARIABLE, na.rm =
TRUE))
#line graph for different countries over time
line = ggplot(data=avg_price, aes(x=anydate(Month), y=VARIABLE,
group=Country)) +
xlab("Date")+
ylab(variable_name_string)+
geom_line(aes(color=Country), size = 1)+
theme_light()
#boxplot over different years
avg_price2 = avg_price
avg_price2$Month = format(as.Date(anydate(avg_price$Month), "%Y-%m-%d"),
"%Y")
box = ggplot(avg_price2, aes(x = Month, y=VARIABLE, fill = Month)) +
geom_boxplot()+
xlab("Date")+
ylab(variable_name_string)+
guides(fill=FALSE)+
theme_light()
var_name = grid.text(variable_name_string, gp=gpar(fontsize=20))
#merge plot into one window
grid.arrange(var_name, hist, line, box, ncol=2)
This works fine for one variable, but now I want to do this for every variable in my dataframe and save the merged plot window for all variables. I have been looking for almost the entire day but I cannot find a solution. Can anyone help me?
Without reproducible example it is hard to help, but you could try to wrap your plotting code in a function and use lapply to repeatedly call the function for all your variables.
make_plots <- function (variable_string) {
var_quo <- rlang::sym(variable_string)
hist = qplot(!!var_quo, data = full_data_noNO, geom="histogram",
fill=I("lightblue"))+
theme_light()
avg_price = full_data_noNO %>%
group_by(Month, Country) %>%
dplyr::summarize(avg = mean(!!var_quo, na.rm =
TRUE))
#line graph for different countries over time
line = ggplot(data=avg_price, aes(x=anydate(Month), y=!!var_quo,
group=Country)) +
xlab("Date")+
ylab(variable_string)+
geom_line(aes(color=Country), size = 1)+
theme_light()
#boxplot over different years
avg_price2 = avg_price
avg_price2$Month = format(as.Date(anydate(avg_price$Month), "%Y-%m-%d"),
"%Y")
box = ggplot(avg_price2, aes(x = Month, y=!!var_quo, fill = Month)) +
geom_boxplot()+
xlab("Date")+
ylab(variable_string)+
guides(fill=FALSE)+
theme_light()
var_name = grid.text(!!var_quo, gp=gpar(fontsize=20))
#merge plot into one window
combined <- grid.arrange(var_name, hist, line, box, ncol=2)
# Save combined plot at VARIABLE_plots.pdf
ggsave(paste0(variable_string, "_plots.pdf"), combined)
combined
}
# Make sure to pass the variable names as character vector
plots <- lapply(c("VARIABLE1", "VARIABLE2"), make_plots)
# OR
plots <- lapply(colnames(full_data_noNO), make_plots)
# Plots can also be accessed and printed individually
print(plots[["VARIABLE1"]])

Resources