Related
I am currently plotting data using the ggpubr package in R (based on ggplot2). When I plot the means of two conditions including standard errors, the y-axis should be limited from 1 to 7, which I indicate using:
p <- ggline(data, x = "condition", y = "measure",
add = c("mean_se"),
ylab = "Measure")
ggpar(y, ylim = c(1, 7), ticks=T, yticks.by = 1)
In the final plot, however, the y-axis shows only values from 1 to 6
I tried to plot the same data using native ggplot2, but the problem persists, once I change the layout.
For ggplot2 I used:
p <- ggplot(data, aes(x=condition, y=measure)) +
geom_line() +
geom_point()+
geom_errorbar(aes(ymin=measure-se, ymax=measure+se), width=.2, position=position_dodge(0.05)) +
ylab("measure") +
xlab("Condition")
p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
p + theme_classic()
It would be great if someone could help me with this issue.
Edit:
as suggested in the comments, here is the data I am trying to plot using ggplot2:
structure(list(condition = structure(3:4, .Label = c("IC", "SC",
"ILC", "SLC"), class = "factor"), measure = c(4.10233918128655, 3.83040935672515
), se = c(0.235026318386523, 0.216811675834834)), class = "data.frame", row.names = c(NA,
-2L))
I think I got something resembling your plot with correct y-axes with the following code:
ggplot(data, aes(x = condition, y = measure)) +
geom_point() +
geom_errorbar(aes(ymin = measure-se, ymax = measure+se),
width = .2, position = position_dodge(0.05)) +
# Group prevents geom_line interpreting each x-axis point as it's own group
geom_line(aes(group = rep(1, nrow(data)))) +
xlab("Condition") +
# Expand is optional, it prevents padding beyond 1 and 7
scale_y_continuous(name = "measure",
limits = c(1, 7),
breaks = 1:7,
expand = c(0,0)) +
theme_classic()
The solution is much more trivial. You were doing everything right! Except for one clerical error. Here is what was happening:
First, you generate your initial plot, fine.
p <- ggplot(data, aes(x=condition, y=measure)) +
geom_line() + geom_point() +
geom_errorbar(aes(ymin=measure-se, ymax=measure+se),
width=.2, position=position_dodge(0.05)) +
ylab("measure") +
xlab("Condition")
This plot does not have the limits. When you add the limits and display it, the scales are correct:
p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
However, note that p did not change! You did not store the result of adding the limits to p. Therefore, p is still without the scale_y_continuous. No wonder then that when you type
p + theme_classic()
...the limits are gone. However, if you try
p <- p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
p + theme_classic()
everything will be correct.
I have a data consisting of 14 different time periods where I would like to plot it in a way that viewer can see where the 14 periods lie. I used to achieve this through using different colors
mycolors = c(brewer.pal(name="Set2", n = 7), brewer.pal(name="Set2", n = 7))
ggplot(derv, aes(x=Date, y=derv, colour = Season)) +
geom_point() +
geom_abline(intercept = 0, slope = 0) +
geom_abline(intercept = neg.cut, slope = 0) +
geom_abline(intercept = pos.cut, slope = 0) +
scale_color_manual(values = mycolors) + ggtitle(" Derivative")+ylab("Derivative")
I have used the above code to product such as plot but now in a new report, I can only use black and white scheme. So I am wondering how I can plot such a plot in R. I have thought of using alternating line types for the 14 different time periods but I do not how to achieve through ggplot. I have tried the following code, but it does not work.The line type stayed the same.
ggplot(derv, aes(x=Date, y=derv)) +
geom_line() +
geom_abline(intercept = 0, slope = 0) +
geom_abline(intercept = neg.cut, slope = 0) +
geom_abline(intercept = pos.cut, slope = 0) +
#scale_color_manual(values = mycolors) + ggtitle("S&P 500 (Smoothed) Derivative") + ylab("Derivative")+
scale_linetype_manual(values = c("dashed","solid","dashed","solid","dashed","solid","dashed",
"solid","dashed","solid","dashed","solid","dashed","solid"))
If you need to show where season changes, couldn't you just use an alternating linetype or alternating point marker? See below for two examples. You can play around with different point markers and linetypes to get the look you want. For more on creating linetypes, see this SO answer. For more on additional point markers (beyond the standard one available through pch), see, for example, here and here. I've also included a way to add the three horizontal lines with less code.
# Fake data
x = seq(0,2*pi,length.out=20*14)
dat=data.frame(time=x, y=sin(x) + sin(5*x) + cos(2*x) + cos(7*x),
group=0:(length(x)-1) %/% 20)
ggplot(dat, aes(time, y)) +
geom_hline(yintercept=c(-0.5,0,0.5), colour="grey50") +
geom_point(aes(shape=factor(group), size=factor(group))) +
scale_shape_manual(values=rep(c(3,15),7)) +
scale_size_manual(values=rep(c(2,1.5),7)) +
theme_bw() + guides(shape=FALSE, size=FALSE)
ggplot(dat, aes(time, y, linetype=factor(group))) +
geom_hline(yintercept=c(-0.5,0,0.5), colour="grey50") +
geom_line(size=0.8) +
scale_linetype_manual(values=rep(1:2,7)) +
theme_bw() + guides(linetype=FALSE)
Thi is my data:
x <- c("22-01-16","26-01-16","28-01-16","01-02-16","05-02-16","16-02-16","17-03-16","18-03-16","04-04-16","05-04-16","06-04-16","08-04-16")
y <- c(97.14,75,54.44,70.45,110.56,66.3,178.76,171.90,419.41,424,518.63,242.17)
z <- c("ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP")
So I make the dataframe
Datos <- data.frame(x)
Datos$Caudal <- y
Datos$Tipo <- z
Datos$Fecha <- as.Date(Datos$x, "%d-%m-%y")
and plot using ggplot2
Serie_Caudal <-
ggplot(Datos, aes(Fecha, Caudal)) +
geom_line(size=1, colour="red") +
geom_point(shape=23,size=1, colour="blue",fill = "blue") +
scale_x_date(date_breaks = "1 week",labels = date_format("%d/%b"))+
xlab("Fecha") + ylab(bquote('Caudal ('*m^3~s^-1*')')) +
ggtitle("Caudales Diarios (01-06/2016)")
Serie_Caudal
I try to plot a legend but i can´t the way, i try use Melt but my data change in a way i can´t plot. Also try scale_fill_manual but the legend don´t show up. I want to know if there is a way to put a legend manualy.
The legend must show a blue point and ADCP
This shows only a blue dot.
ggplot(aes(Fecha, Caudal, colour = "ADCP"), data = Datos) +
geom_point() +
geom_point(shape=23,size=1,color="blue",fill = "blue") +
scale_color_manual(values = c("ADCP"="blue"),name = "") +
geom_line(color="red", size=1) +
scale_x_date(date_breaks = "1 week",labels = date_format("%d/%b")) +
xlab("Fecha") + ylab(bquote('Caudal ('*m^3~s^-1*')')) +
ggtitle("Caudales Diarios (01-06/2016)")
I want to fill the box with its color (like: blue ->blue).
goo1 <- ggplot(dataset1, aes(x=Color, y=Scored.Probabilities)) +
geom_boxplot() +
stat_summary(fun.y = mean, geom="point",colour="darkred", size=3) +
stat_summary(fun.data = fun_mean, geom="text", vjust=-0.7)
print (goo1)
Data can be found here: link
Add a scale (although you need to make sure first that every level of dataset1$Persons is indeed the name of a color recognized by R ; currently, dark and game value are not)
colors <- levels(dataset1$Color)
colors[colors == "dark"] <- "black"
colors[colors == "game value"] <- "cyan"
goo2 <- ggplot(dataset1, aes(x=Color, y=Scored.Probabilities)) +
geom_boxplot(aes(fill=Color)) +
stat_summary(fun.y = mean, geom="point",colour="darkred", size=3)
goo2 <- goo2 + scale_fill_manual(values=colors)
Okay so here is my code for a base map:
gg <- ggmap(Peru) +
geom_map(data = peru.coast, map = peru.coast, aes(x = long, y = lat, map_id = region),
fill="gray", color="black") +
xlim(-86, -70) +
ylim(-20, -4) +
labs(x = "Longitude", y = "Latitude") +
coord_map()
I then add in towns I wish to name manually (wasn't sure how to do it using google maps as I only wanted these 4)
gg <- gg + geom_point(aes(x=-78.981885, y=-8.229354, size=3)) +
annotate("text", label='Salaverry', size=4, x=-77.2, y=-8.229354) +
geom_point(aes(x=-71.345838, y=-17.644347, size=3)) +
annotate("text", x=-70.545838, y=-17.644347, label = 'Ilo', size=4) +
geom_point(aes(x=-77.142375, y=-12.047544, size=3)) +
annotate("text", x=-75.9, y=-12.047544, label = 'Callao', size=4) +
geom_point(aes(x=-78.610677, y=-9.074166, size=3)) +
annotate("text", x=-76.9, y=-9.074166, label = 'Chimbote', size=4)
gg <- gg + guides(size=FALSE) #this removes the legend with the black dot and '3' on it
gg
I get this lovely map:
I then use this dataset to add datapoints, and I wish to make the points bigger or smaller according to 'n' abundance
Trip_Set sex Set.Lon Set.Lat n
119_1 hembra -81.09390 -9.32338 2
119_7 hembra -81.03117 -9.09622 1
161_3 macho -83.76533 -9.74533 5
193_8 hembra -81.00888 -9.00950 7
255_5 macho -80.14992 -8.64592 1
271_6 hembra -72.20233 -18.05117 6
271_6 macho -72.20233 -18.05117 7
328_7 hembra -78.66667 -12.91700 2
403_3 hembra -80.03037 -10.03900 1
428_2 hembra -83.01305 -8.74883 2
655_4 hembra -71.58363 -18.24882 1
using this code:
ggAB <- gg + geom_point(data=dframe4, aes(Set.Lon, Set.Lat, colour='red', size=n))
ggAB <- ggAB + theme(legend.title = element_text(colour="black", size=12, face="bold"))
ggAB <- ggAB + guides(colour=FALSE) #This removes the legend for the red colour
ggAB <- ggAB + scale_size(name='Sharks per line', range = c(5,9))
ggAB <- ggAB + theme(legend.key=element_rect(fill = NA)) #This removes the boxes around the points
ggAB
However, when I do this... I get this:
The datapoints are plotted great (phew!), but why does it make the points bigger for my town names? I can't seem to get it to just keep the abundance for my 'n' number datapoints... It also doesn't put an automatic legend on (as ggplot usually does), even when I try and put one in manually using the scale_discrete function.
I thought it might be something to do with the fact that I use gg + guides(size=FALSE) in the first part, but even when taking that out it doesn't work, but adds in an annoying legend for my town datapoints.
Any ideas?
The problem is that in the code where you add the towns, you have put the size inside the aes. Therefore it also gets transformed when you call scale_size(name='Sharks per line', range = c(5,9)). Just use size outside the aes:
gg <- gg + geom_point(aes(x=-78.981885, y=-8.229354), size=3) +
annotate("text", label='Salaverry', size=4, x=-77.2, y=-8.229354) +
geom_point(aes(x=-71.345838, y=-17.644347), size=3) +
annotate("text", x=-70.545838, y=-17.644347, label = 'Ilo', size=4) +
geom_point(aes(x=-77.142375, y=-12.047544), size=3) +
annotate("text", x=-75.9, y=-12.047544, label = 'Callao', size=4) +
geom_point(aes(x=-78.610677, y=-9.074166), size=3) +
annotate("text", x=-76.9, y=-9.074166, label = 'Chimbote', size=4)
In addition to #shadow's answer, I'd like to leave the following for the OP as supplementary information. This comes from the chat with the OP. If you want to avoid using annotate, you can use geocode in the ggmap package. Here, I added some from my answer to the previous question of the OP, and I combined/modified the OP's code. One change is that I used alpha so that you can see red/pink points in the ocean. One note is that the positions of the city names are not perfect; the further you go down south in the map, the more you see gaps between points and city names. This could be due to something to do with map projection. According to Wiki, googlemap is using something close to marcator, but not exact the same. This could be the reason. Some GIS experts here would be able to provide more information.
library(ggmap)
library(mapdata)
library(ggplot2)
# Get Peru map
Peru <- get_map(location = "Peru", zoom = 5, maptype="satellite")
# This is the layer I wish to put over the top
coast_map <- fortify(map("worldHires", fill = TRUE, plot = FALSE))
# Subset data for Peru
peru.coast <- subset(coast_map, region == "Peru")
### Get lon and lat using geocode() in the ggmap package and crate a data frame
cities <- c("Salaverry", "Chimbote", "Callao", "Ilo")
locations <- geocode(cities)
locations$city <- cities
locations2 <- transform(locations, lon2 = lon + 1.1) # This is for text position
ggmap(Peru) +
geom_map(data = peru.coast, map = peru.coast, aes(x = long, y = lat, map_id = region),
fill="gray", color="black") +
geom_point(data = locations2, aes(x = lon, y = lat, color = city), size = 4) +
geom_text(data = locations2, aes(x = lon2, y = lat, label = city), size = 3) +
scale_color_manual(values = rep(c("black"), times = 4)) +
geom_point(data = newdata, aes(Set.Lon, Set.Lat, size = n), colour = "red", alpha = 0.5) +
scale_size(name = "Sharks per line", range = c(5,9)) +
xlim(-86, -70) +
ylim(-20, -4) +
labs(x = "Longitude", y = "Latitude") +
coord_map("mercator") +
guides(colour=FALSE) +
theme(legend.key=element_rect(fill = NA))