This question already has answers here:
Plotting two variables as lines using ggplot2 on the same graph
(5 answers)
Closed 3 years ago.
I am trying to plot multiple line on one chart.
They all have the same X- axis in months, but different observation for y-axis.
I have tried writing this code but I keep on getting an error.
Can someone point me towards what I am doing wrong?
"Test3" is the same of my data set, "Oil_1" represents the first Y observation, "Oil_2" second observation and "Month" is the X-axis
ggplot(test3, + aes(x = Months)) +
geom_line(aes(y=oil_1),colour="blue")+
geom_line(aes(y=oil_2),colour="red") +
ylab(label="Production")+
xlab("Months")
You have an extra "+" before the first aesthetic call, that could be the problem.
Related
This question already has answers here:
Plot with multiple lines in different colors using ggplot2
(1 answer)
ggplot combining two plots from different data.frames
(3 answers)
Multiple plots with variable color in R ggplot
(1 answer)
Closed 6 months ago.
I have 2 dataframes, "hot", and "cold" that I want to plot on the same ggplot scatterplot, in 2 different colours.
I want the "temperature" variable on the x-axis and "depth" variable on the y-axis.
I then want both dataframes plotted on this graph, with "hot" shown by red points and "cold" shown by blue points.
I have already plotted them individually, but can't figure out how to combine them into one plot. Example of the code used for the individual plots:
ggplot(hot, aes(x=temp, y=depth)) +
geom_point(size=0.1) +
labs(title="Depth Profile of Temperature in the Warm Months",x="Temperature (degC)", y = "Depth (m)") +
scale_y_reverse()
This question already has answers here:
Bar plot with facets and same bar size (binwidth) with option to shrink the panel size
(1 answer)
ggplot2 different facet width for categorical x-axis [duplicate]
(1 answer)
ggplot2 facet_grid How to create different x-axis with keeping all values in each panel?
(1 answer)
Closed 12 months ago.
I am trying to recreate a bar graph (below) that has both stacked values (Yes/No/Not responding) and grouped values (e.g., the three bars under "Group activity" and the three bars under "In-cell activities").
I currently have a stacked graph using the following code
ggplot(f15dta,aes(x=number,y=factor(activity,level=rev(activities)),fill=fill,order=desc(fill)))+
geom_bar(stat="identity")+
guides(fill=guide_legend(reverse=T))
which produces the below stacked graph.
I have another variable in the data frame for the groups I want (e.g., "Group activity"). I tried using facets, but it looks like a complete mess (there are five groups/facets I want, but it graphs all the different bar graphs onto each one, so there's 90 bars instead of 18--see below)
Does anyone have any ideas on how to better approach stacked and grouped graphs in ggplot? I feel like such graphs have to be fairly common, but am unsure how to proceed. Normally I would group using fill and position=dodge, but since I am stacking with fill, I am slightly unsure what to do. Thanks for the help.
This question already has answers here:
Using ggplot2, can I insert a break in the axis?
(10 answers)
Closed 2 years ago.
I want to make a plot with not equally spaced axis. Here is my data.
group <- c("group1","group2","group3","group4","group5","group6")
value <- c(520,550,13,15,30,20)
df <- cbind.data.frame(group,value)
I want to make a plot like this.
idealplot
I tried to make it using ggplot, like this.
ggplot(df,aes(x=factor(group),y=value))+geom_bar(stat="identity")
This code makes a plot with equally spaced axis which I do not want. This plot is unable to compare group 3,4,5 and 6. If you know how to make the ideal plot, please let me know.
undesiredplot
I don't think I've ever seen a discontinuous axis in ggplot2.
With points instead of bars, you can fake a discontinuous axis by using facets:
ggplot(df,aes(x=factor(group),y=value)) +
facet_grid(value < 100 ~., scales='free_y') +
geom_point(stat="identity") +
theme(strip.text = element_blank())
Other alternatives to visualizing data with large differences in values is to log-transform or sqrt-transform the Y axis, which effectively stretches out small values and compresses large values.
ggplot(df,aes(x=factor(group),y=value)) +
geom_bar(stat="identity") +
scale_y_log10() +
annotation_logticks(base=10, sides='lr')
This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 6 years ago.
I have a data frame df1 and want to draw a barplot of AccountExecutive and their corresponding ClearRate where the bars are arranged so that it is decreasing from left to right.
I tried this code but the resulting graph still reflects AccountExecutive order as it appears in df1
ggplot(arrange(df1, -ClearRate), aes(x = AccountExecutive, y = ClearRate)) +
geom_bar(stat="identity")
Can anyone help me correcting this code?
NOTE: Not a duplicate of the previous question because that one asks for an arbitrary positioning of the x axis labels. This question asks how to sort x-axis labels considering their y-axis values.
Try this one the code below should reorder AE according to clearance rate
ggplot(df1,aes(x=reorder(AccountExecutive,-ClearRate),y=ClearRate))+geom_bar(stat"identity")
here is the more about reorder function
Reorder bars in geom_bar ggplot2
This question already has answers here:
ggplot2 - jitter and position dodge together
(2 answers)
Closed 3 years ago.
Is it possible to group the jitter in a boxplot like mine so that the data points line up with the factor for each market? Right now it's lining up by market name. I colored them to show which ones should be grouped.
My code
p<-ggplot(droplevels(subset(sData,STORE_TYPE=='Test')),aes(factor(MARKET_NAME),DST_UNITS))
p +
geom_boxplot(aes(fill=factor(PROGRAM_STATUS,c("PRE-PROGRAM","POST-PROGRAM")), outlier.shape=NA) +
geom_jitter(aes(color=factor(PROGRAM_STATUS,c("PRE-PROGRAM","POST-PROGRAM"))),position=position_jitter(width=0))
Solution provided in comments by Didzis Elferts and link to this question
sData<-droplevels(subset(sData,STORE_TYPE=='Test'))
ggplot(sData,aes(x=factor(MARKET_NAME),y=DST_UNITS,fill=factor(PROGRAM_STATUS,c("PRE-PROGRAM","POST-PROGRAM")))) +
geom_boxplot(outlier.shape=NA) +
geom_point(position=position_jitterdodge())