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()
Related
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 an answer here:
ggplot2 geom_bar - how to keep order of data.frame [duplicate]
(1 answer)
Closed 2 years ago.
I want to change the order of the stacked bar plot in R? I am sharing the example below and the green bar will be under the blue bar, how can I can this order? See the code below.
d1<-data.frame(Gene=c("DNA","DNA",
"RNA","RNA",
"XX","XX"),
Gender=c("M","F","M","F","M","F"),
p_value=c( 0.5, 0.1,
0.6,0.01,
0.07,0.02
))
p<-d1 %>%
ggplot(aes(x=forcats::fct_reorder(Gene,p_value), y=p_value, fill=Gender)) +
geom_col(color="black",position=position_dodge()) +
coord_flip() +
scale_fill_manual(values=c('#6495ED','#2E8B57'))+
labs(x="Gene", y="p-value")
Just convert d1$Gendered into a factor and specify the levels in the order you want them.
d1$Gender <- factor(d1$Gender, levels = c("M", "F"))
Then, run the code to create your plot.
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.
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 an answer here:
How to change panel labels and x-axis sublabels in a lattice bwplot
(1 answer)
Closed 8 years ago.
I am using the following histogram command to visualize the features of a labeled dataset that has binary labels (0 or 1).
require(lattice)
data <- data.frame(num_child=1:10,label=rep(0:1,each=5))
histogram( ~ data$num_child | data$label ,xlab="Number of children")
I get a pair of histogram plots, as expected, with x-axis labeled as "Number of children" and y-axis labeled as "Percent of Total". However, the labels on top of both the plots are "data$label" rather than the value of the group label. The histogram command takes a xlab, and ylab as parameter, but does not seem to have a parameter for the group label. How can I get the group label (i.e. "0" and "1") to be printed?
Looks like the easiest solution is to change your grouping to a factor:
histogram( ~ data$num_child | as.factor(data$label),xlab="Number of children")