I need to plot a line chart that contains 2 lines, as per the dataset below:
I need each line of the chart to match the Technique. The values of X and Y are Release and Added respectively. The graph I need to generate is similar to the one in the figure below:
To plot the first line, I tried:
plot(IrisChangeModules[IrisChangeModules$Technique=="aop"]$Added, IrisChangeModules[IrisChangeModules$Technique=="aop"]$Release, type = "l")
Using ggplot (not a base r solution though), one way of getting your desired output can be:
library(ggplot2)
ggplot(dataset, aes(x=Release, y=Added, group=Technique)) + geom_line(aes(linetype = Technique, color=Technique)) + geom_point(aes(color = Technique)) + theme_bw()
The output given your dummy data looks:
You can play with the different parameters of ggplot to position the legend and other aspects of the plot.
UPDATE:
Remove ylab("Added Modules") + scale_color_discrete(name = "SPL Techniques") from your script and save the output on a variable as follows:
x <- ggplot(IrisChangeModules, aes(x=Release, y=Added, group=Technique)) + geom_line(aes(linetype = Technique, color=Technique)) + geom_point(aes(color = Technique)) + theme_bw()
Then overlay your modified label and new legend title as:
x+ labs(y = "Added Modules") + scale_fill_discrete(name = "SPL Techniques")
That'll give you what you want.
Firstly create an ordered release int
IrisChangeModules$release_n = 1:nrow(IrisChangeModules)
Now create a blank plot, type="n" means nothing is actually plotted.
plot(Added~release_n,data=IrisChangeModules,type="n",xaxt = "n")
Create axis labels with the original variable ( you may need to update the at if you have more than 4 releases).
axis(1,at=1:4,labels=IrisChangeModules$Release)
Add lines are required
lines(Added~release_n,data=IrisChangeModules[IrisChangeModules$tech=="dop",],type='l',col="green")
lines(Added~release_n,data=IrisChangeModules[IrisChangeModules$tech=="aop",],type='l',col="red")
Add a legend, make sure you are updating if you add more lines
legend(1,90,
legend=c("dop", "aop"),
col=c("green", "red"),
lty=1,
cex=0.8)
Related
I have a two small sets of points, viz. (1,a1),...,(9,a9) and (1,b1),...,(9,b9). I'm trying to interpolate these two set of points separately by using splines with the help of ggplot2. So, what I want is 2 different splines curves interpolating the two sets of points on the same plot (Refer to the end of this post).
Since I have a very little plotting experience using ggplot2, I copied a code snippet from this answer by Richard Telford. At first, I stored my Y-values for set of points in two numeric variables A and B, and wrote the following code :
library(ggplot2)
library(plyr)
A <- c(a1,...,a9)
B <- c(b1,...,b9)
d <- data.frame(x=1:9,y=A)
d2 <- data.frame(x=1:9,y=B)
dd <- rbind(cbind(d, case = "d"), cbind(d2, case = "d2"))
ddsmooth <- plyr::ddply(dd, .(case), function(k) as.data.frame(spline(k)))
ggplot(dd,aes(x, y, group = case)) + geom_point() + geom_line(aes(x, y, group = case), data = ddsmooth)
This produces the following output :
Now, I'm seeking for an almost identical plot with the following customizations :
The two spline curves should have different colours
The line width should be user's choice (Like we do in plot function)
A legend (Specifying the colour and the corresponding attribute)
Markings on the X-axis should be 1,2,3,...,9
Hoping for a detailed solution to my problem, though any kind of help is appreciated. Thanks in advance for your time and help.
You have already shaped your data correctly for the plot. It's just a case of associating the case variable with colour and size scales.
Note the following:
I have inferred the values of A and B from your plot
Since the lines are opaque, we plot them first so that the points are still visible
I have included size and colour parameters to the aes call in geom_line
I have selected the colours by passing them as a character vector to scale_colour_manual
I have also selected the sizes of the lines by calling scale_size_manual
I have set the x axis breaks by adding a call to scale_x_continuous
The legend has been added automatically according to the scales used.
ggplot(dd, aes(x, y)) +
geom_line(aes(colour = case, size = case, linetype = case), data = ddsmooth) +
geom_point(colour = "black") +
scale_colour_manual(values = c("red4", "forestgreen"), name = "Legend") +
scale_size_manual(values = c(0.8, 1.5), name = "Legend") +
scale_linetype_manual(values = 1:2, name = "Legend") +
scale_x_continuous(breaks = 1:9)
Created on 2020-07-15 by the reprex package (v0.3.0)
I am trying to generate density plot with two overlaid distributions using ggplot2. My data looks like:
diag_elements <- data.frame(x = c(diag(Am.dent), diag(Am.flint)),
group=rep(c("Dent", "Flint"), c(length(diag(Am.dent)), length(diag(Am.flint)))))
And my call to ggplot is:
ggplot(diag_elements) +
geom_density(aes(x=x, colour=group, fill=group), alpha=0.5) +
labs(x = "Diagonal elements of the matrix", y = "Density", fill = "Heterotic Group") +
theme(legend.position = c(0.85, .75))
However, instead of simply renaming the legend with the more complete name specified in fill, this generates a second legend:
Does anyone have any suggestions for getting this same graph, but without the improperly formatted legend?
Thanks!
The other option is guides which allows specific removal of certain legneds. You simply add to your ggplot
+guides(color=FALSE)
I'm trying to make a plot with five separate lines that represent different variables (i.e., columns). I've been trying to fix the legend and I can't get it right. The colors also seem to be incorrect, so I'm confused as to how to proceed. Data, code, and current plot are below:
test.dat <- data.frame(matrix(c(0,.072,.063,.062,.059,.055,1,.029,.035,.024,.036,.017),
nrow=2,byrow=T))
colnames(test.dat) <- c("X1","Y1","Y2","Y3","Y4","Y5")
test.plot <- ggplot(data=test.dat) +
geom_line(aes(x=X1, y=Y1, color="darkred")) +
geom_line(aes(x=X1, y=Y2, color="darkgreen")) +
geom_line(aes(x=X1, y=Y3, color="darkblue")) +
geom_line(aes(x=X1, y=Y4, color="darkorange")) +
geom_line(aes(x=X1, y=Y5, color="darkgoldenrod"))
I'm trying to make the variable names (i.e., Y1, Y2, etc.) the names for the lines on the legend as well as name the legend itself. I've googled around to figure out how to change the legend, but nothing I've tried changes the plot. I also need to fix the the axis labels and add a plot title.
I'm fairly new to ggplot, so a point in the right direction would be much appreciated. Please let me know if I can clarify anything.
It is easier if you change your data from wide to long, and then plot the lines. You are also defining the colors within the aes() parameter which does not change the color, but, in effect, creates a variable with that value.
You did not mention exactly what you want to do with your axis labels, but they can be edited using scale_x_continuous or scale_y_continuous
library(tidyverse)
test.dat <- data.frame(matrix(c(0,.072,.063,.062,.059,.055,1,.029,.035,.024,.036,.017),
nrow=2,byrow=T))
colnames(test.dat) <- c("X1","Y1","Y2","Y3","Y4","Y5")
test.dat %>%
gather(Y_var, value, -X1) %>%
ggplot(aes(x = X1, y = value, color = Y_var)) +
geom_line() +
scale_color_manual(
values = c("darkred","darkgreen","darkblue","darkorange",
"darkgoldenrod")
) +
labs(title = "Some plot title",
color = "Some legend title")
I have a ggplot, which is a combination of a stacked graph and line graph
ggplot() +
geom_bar(data=smr2, aes(x=Pract, y=value, fill=variable), stat='identity') +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
geom_line(data=summarised[,1:3], aes(x=Pract,y=YTDTarget, group=1),size = 1) +
geom_point(data=summarised[,1:3], mapping = aes(x = Pract, y = YTDTarget),size=2.5)+
geom_text_repel(data=summarised[,1:3], aes(x=Pract,y=YTDTarget,label=YTDTarget), size = 5)
I want to add the legend for line graph. But the part group=1 seems to prevent this.
The graph I created is as
Also, please help to change the name of the legend from variable to "Actuals"
This graph is for compare the Target(line graph) against actually achieved(stacked Bar).
Please try this:
To geom_line add dummy variable (to add it to legend - in this case I'm using linetype).
geom_line(data = summarised[,1:3],
aes(Pract, YTDTarget, group = 1, linetype = ""),
size = 1)
To change legend name add labs() to your plot.
labs(fill = "Actuals",
linetype = "My Line Name")
I am trying to simply add a legend to my Nyquist plot where I am plotting 2 sets of data: 1 is an experimental set (~600 points), and 2 is a data frame calculated using a transfer function (~1000 points)
I need to plot both and label them. Currently I have them both plotted okay but when i try to add the label using scale_colour_manual no label appears. Also a way to move this label around would be appreciated!! Code Below.
pdf("nyq_2elc.pdf")
nq2 <- ggplot() + geom_point(data = treat, aes(treat$V1,treat$V2), color = "red") +
geom_point(data = circuit, aes(circuit$realTF,circuit$V2), color = "blue") +
xlab("Real Z") + ylab("-Imaginary Z") +
scale_colour_manual(name = 'hell0',
values =c('red'='red','blue'='blue'), labels = c('Treatment','EQ')) +
ggtitle("Nyquist Plot and Equivilent Circuit for 2 Electrode Treatment Setup at 0 Minutes") +
xlim(0,700) + ylim(0,700)
print(nq2)
dev.off()
Ggplot works best with long dataframes, so I would combine the datasets like this:
treat$Cat <- "treat"
circuit$Cat <- "circuit"
CombData <- data.frame(rbind(treat, circuit))
ggplot(CombData, aes(x=V1, y=V2, col=Cat))+geom_point()
This should give you the legend you want.
You probably have to change the names/order of the columns of dataframes treat and circuit so they can be combined, but it's hard to tell because you're not giving us a reproducible example.