This is very similar to this question (link), but I'm not quite sure how to manipulate it for my needs.
I have a faceted plot with two panels, and I would like to label three quadrants in the first panel and only the first panel.
Here is a mock data set:
dfr=data.frame(
variable=rep(c("A","B"),each=2),
x=c(2,-3,4,-5),
y=c(-2,4,-2,6))
And here is the plot:
p=ggplot(dfr,aes(x,y))+
geom_point()+
facet_grid(variable~.)+
scale_x_continuous(limits=c(-6,6))+
scale_y_continuous(limits=c(-6,6))+
geom_hline(yintercept=0)+
geom_vline(xintercept=0)
This is what I would like to accomplish:
You can always create a separate data frame with the desired labels and plot them using geom_text:
dfLab <- data.frame(variable = rep("A",3),
x = c(3,3,-3),
y = c(3,-3,-3),
lab = c('I','IV','III'))
ggplot(dfr,aes(x,y))+
geom_point()+
facet_grid(variable~.)+
scale_x_continuous(limits=c(-6,6))+
scale_y_continuous(limits=c(-6,6))+
geom_hline(yintercept=0)+
geom_vline(xintercept=0) +
geom_text(data = dfLab,aes(x=x,y=y,label=lab))
Related
I am trying to create a simple dotplot that contains 100+ points, some of which are grouped close together. I need the points to be individually labeled, but I would like to stack the labels for points that are close together on top of each other.
Basically, I would like to create a similar dotplot with labeling to the graph below.
As a code example, consider the following code where I would like to add the car name to the dotplot in a way similar to the graphic.
ggplot(mtcars, aes(x = mpg)) +
geom_dotplot(binwidth = .4, stackdir = "centerwhole") +
scale_y_continuous(NULL, breaks = NULL)
I want to draw a combined bar plot, so that I can make comparision among different score types.
compare_data = data.frame(model=c(lr,rf,gbm,xgboost),
precision=c(0.6593,0.7588,0.6510,0.7344),
recall=c(0.5808,0.6306,0.4897,0.6416),f1=c(0.6176,0.6888,0.5589,0.6848),
acuracy=c(0.6766,0.7393,0.6453,0.7328))
compare1 <- ggplot(evaluation_4model, aes(x=Model, y=Precision)) +
geom_bar(aes(fill = Model), stat="identity")
compare1 <- compare+labs(title = "Precision")
Here is one of the barplot I draw, and this is the type of "precision", however, I want to make a wide bar plot, with all the models under 4 score types sharing the same Y-axis, also with subtitle if possible.
Your code throws an error, because evaluation_4model is not defined.
However, the answer to your problem is likely to make a faceted plot and hence melt the data to a long format. To do this, I usually make use of the reshape library. Tweaking your code looks like this
library(ggplot2)
library(reshape2)
compare_data = data.frame(model=c("lr","rf","gbm","xgboost"),
precision=c(0.6593,0.7588,0.6510,0.7344),
recall=c(0.5808,0.6306,0.4897,0.6416),
f1=c(0.6176,0.6888,0.5589,0.6848),
acuracy=c(0.6766,0.7393,0.6453,0.7328))
plotdata <- melt(compare_data,id.vars = "model")
compare2 <- ggplot(plotdata, aes(x=model, y=value)) +
geom_bar(aes(fill = model), stat="identity")+
facet_grid(~variable)
compare2
does that help?
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.
I'm trying to add a legend to a plot that I've created using ggplot. I load the data in from two csv files, each of which has two columns of 8 rows (not including the header).
I construct a data frame from each file which include a cumulative total, so the dataframe has three columns of data (bv, bin_count and bin_cumulative), 8 rows in each column and every value is an integer.
The two data sets are then plotted as follows. The display is fine but I can't figure out how to add a legend to the resulting plot as it seems the ggplot object itself should have a data source but I'm not sure how to build one where there are multiple columns with the same name.
library(ggplot2)
i2d <- data.frame(bv=c(0,1,2,3,4,5,6,7), bin_count=c(0,0,0,2,1,2,2,3), bin_cumulative=cumsum(c(0,0,0,2,1,2,2,3)))
i1d <- data.frame(bv=c(0,1,2,3,4,5,6,7), bin_count=c(0,1,1,2,3,2,0,1), bin_cumulative=cumsum(c(0,1,1,2,3,2,0,1)))
c_data_plot <- ggplot() +
geom_line(data = i1d, aes(x=i1d$bv, y=i1d$bin_cumulative), size=2, color="turquoise") +
geom_point(data = i1d, aes(x=i1d$bv, y=i1d$bin_cumulative), color="royalblue1", size=3) +
geom_line(data = i2d, aes(x=i2d$bv, y=i2d$bin_cumulative), size=2, color="tan1") +
geom_point(data = i2d, aes(x=i2d$bv, y=i2d$bin_cumulative), color="royalblue3", size=3) +
scale_x_continuous(name="Brightness", breaks=seq(0,8,1)) +
scale_y_continuous(name="Count", breaks=seq(0,12,1)) +
ggtitle("Combine plot of BV cumulative counts")
c_data_plot
I'm fairly new to R and would much appreciate any help.
Per comments, I've edited the code to reproduce the dataset after it's loaded into the dataframes.
Regarding producing a single data frames, I'd welcome advice on how to achieve that - I'm still struggling with how data frames work.
First, we organize the data by combining i1d and i2d. I've added a column data which stores the name of the original dataset.
restructure data
i1d$data <- 'i1d'
i2d$data <- 'i2d'
i12d <- rbind.data.frame(i1d, i2d)
Then, we create the plot, using syntax that is more common to ggplot2:
create plot
ggplot(i12d, aes(x = bv, y = bin_cumulative))+
geom_line(aes(colour = data), size = 2)+
geom_point(colour = 'royalblue', size = 3)+
scale_x_continuous(name="Brightness", breaks=seq(0,8,1)) +
scale_y_continuous(name="Count", breaks=seq(0,12,1)) +
ggtitle("Combine plot of BV cumulative counts")+
theme_bw()
If we specify x and y within the ggplot function, we do not need to keep rewriting it in the various geoms we want to add to the plot. After the first three lines I copied and pasted what you had so that the formatting would match your expectation. I also added theme_bw, because I think it's more visually appealing. We also specify colour in aes using a variable (data) from our data.frame
If we want to take this a step further, we can use the scale_colour_manual function to specify the colors attributed to the different values of the data column in the data.frame i12d:
ggplot(i12d, aes(x = bv, y = bin_cumulative))+
geom_line(aes(colour = data), size = 2)+
geom_point(colour = 'royalblue', size = 3)+
scale_x_continuous(name="Brightness", breaks=seq(0,8,1)) +
scale_y_continuous(name="Count", breaks=seq(0,12,1)) +
ggtitle("Combine plot of BV cumulative counts")+
theme_bw()+
scale_colour_manual(values = c('i1d' = 'turquoise',
'i2d' = 'tan1'))
Have an assignment where we need to provide one-dimensional graphs for EDA but the sample code given answers most of the requirements already (simple scatter and box plots and a histogram) so I am trying to "spice it up" a little by creating some more interesting graphs. Only need a couple.
The data set is the twin IQ data across several studies/authors and I was wanting to do a back-to-back histogram of the twins separated by author. So far I can do an overlay of authors or the back to back of the twins using ggplot but I am then stuck when trying to separate in to either 4 graphs or overlaid back-to-backs.
The code I was using for the overlay was ggplot with either geom_density or geom_histogram and the code for the back-to-back came from R-Bloggers and I used the first snippet:
ggplot(df, aes(IQ)) + geom_histogram(aes(x = x1, y = ..density..), fill = "blue") + geom_histogram( aes(x = x2, y = -..density..), fill = "green")
What I am looking for is a way to combine these two techniques or how to get ggplot to split the graphs up by factor in much the same was as plot/lattice does when you do, for example:
bwplot(y~x1.x2|Author, data=df)
The snippet that I am using to achieve separate plots includes facet_grid() such that the final code is:
ggplot(df, aes(y)) + facet_grid(~Author) + geom_histogram(aes(x = x1, y = ..density..), fill = "green") + geom_histogram(aes(x = x2, y = -..density..), fill = "blue")
I wasn't previously aware of the facet_grid() function of ggplot so thank you very much to MLavoie and Brandon Bertelsen.