Related
I have the following data:
df <- structure(list(Site = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 4L,
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Permafrost", "Palsa",
"Palsa Hollow", "Rich Sphagnum Lawn", "Tall Graminoid Fen"), class = "factor"),
Depth = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Upper Depth", "Lower Depth"
), class = "factor"), ug.Al.m2 = c(0.093375394, 0.15684537,
0.025747986, 0.031130205, 0.074247144, 0.054740061, 0.006671475,
0.002208133, 0.003427595, 0.001447068, 0.013960114, 0.008988422,
0.047630561, 0.005434406, 0.041627689, 0.004127627, 0.013713378,
0.00501951, 0.512382579, 0.628336756, 0.293063584, 0.460299194,
0.188002926, 0.385744659, 0.220549738, 0.003135834, 0.006755556,
0.012846966, 0.008662843, 0.0064347, 0.004951768)), row.names = c(NA,
-31L), class = "data.frame")
I am using it to make a barplot:
library (cowplot)
library (ggplot2)
library (RColorBrewer)
X_Axis_Labels <- c("Permafrost", "Palsa", expression(atop("Palsa", "Hollows")), expression(atop("Rich", italic("Sphagnum"), "Lawn")), expression(atop("Tall", "Graminoid", "Fen")))
Legend_Labels <- c("Permafrost", "Palsa", "Palsa Hollows", expression(paste("Rich ", italic("Sphagnum"), " Lawn")), "Tall Graminoid Fen")
Palette1 <- c(brewer.pal(11, "RdBu")[c(11,10,9,8,7)])
ggplot(df, aes(x = Site, y = ug.Al.m2, fill = Site)) +
stat_summary(geom = "bar", width = 0.6, fun = mean, colour = "black") +
stat_summary(geom = "errorbar", width = 0.2, fun.data = mean_se) +
ggtitle("Total Aluminum Concentrations in Permafrost Peatland Communities") +
scale_x_discrete(labels = X_Axis_Labels) +
scale_fill_manual(values = Palette1, labels = Legend_Labels) +
ylab(expression(paste("Aluminum Concentration, ", mu, "g m" ^ "-2"))) +
xlab("Site") +
theme_cowplot(13)
Here's what the graph looks like:
I'm having a lot of trouble getting all three lines of the x axis labels to appear on my graph. The word 'Lawn', which should appear under 'Sphagnum', is lost. Since the word 'Sphagnum' needs to be italicized, I can't simply use the standard line break (\n). I've also tried playing with the plot margins to no avail.
Is there a solution to this problem?
Thank you!
Try this approach with ggtext and element_markdown(). You can use ** for italic and <br> for the break line. You can customize at any level you wish. Here the code:
library (cowplot)
library (ggplot2)
library (RColorBrewer)
library(ggtext)
X_Axis_Labels <- c("Permafrost", "Palsa", "Palsa<br>Hollows", "Rich<br>*Sphagnum*<br>Lawn",
"Tall<br>*Graminoid*<br>Fen")
Legend_Labels <- c("Permafrost", "Palsa", "Palsa Hollows", expression(paste("Rich ", italic("Sphagnum"), " Lawn")), "Tall Graminoid Fen")
Palette1 <- c(brewer.pal(11, "RdBu")[c(11,10,9,8,7)])
ggplot(df, aes(x = Site, y = ug.Al.m2, fill = Site)) +
stat_summary(geom = "bar", width = 0.6, fun = mean, colour = "black") +
stat_summary(geom = "errorbar", width = 0.2, fun.data = mean_se) +
ggtitle("Total Aluminum Concentrations in Permafrost Peatland Communities") +
scale_x_discrete(labels = X_Axis_Labels) +
scale_fill_manual(values = Palette1, labels = Legend_Labels) +
ylab(expression(paste("Aluminum Concentration, ", mu, "g m" ^ "-2"))) +
xlab("Site") +
theme_cowplot(13)+
theme(axis.text.x = element_markdown())
Output:
I know I'm using the dotplot in a slightly odd way, but I've got it producing the graphic I want; which shows how many players in each position each Premier League football club has, with each dot showing one player. I have multiple categories - showing whether the player is a squad player or a youth player, these are plotted separately, with the second nudged down so they don't overlap.
I want to add another layer of information to it, which is shading the dots based on how many minutes each player has played. I have this data in my data frame.
It colour codes the dots perfectly, except when the data is "grouped", in which case it leaves it grey.
I've read the guidance on producing a good r question. I've cut down the data to show the problem, without being huge, and removed all lines of code such as manipulating the data to this point and graph titles etc.
This is a sample of 20 players, which produces 16 nicely coloured dots, and 2 pairs of gray, uncoloured dots.
structure(list(team = structure(c(2L, 3L, 4L, 4L, 5L, 6L, 8L, 9L, 11L, 12L, 5L, 6L, 7L, 10L, 12L, 12L, 1L, 4L, 5L, 7L), .Label = c("AFC Bournemouth", "Arsenal", "Brighton & Hove Albion", "Chelsea", "Crystal Palace", "Everton", "Huddersfield Town", "Leicester City", "Liverpool", "Swansea City", "Tottenham Hotspur", "West Bromwich Albion"), class = "factor"),
role = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "U21", class = "factor"),
name = structure(c(10L, 2L, 1L, 15L, 13L, 19L, 4L, 7L, 20L,
8L, 17L, 9L, 18L, 11L, 3L, 6L, 14L, 5L, 12L, 16L), .Label = c("Boga",
"Brown", "Burke", "Chilwell", "Christensen", "Field", "Grujic",
"Harper", "Holgate", "Iwobi", "Junior Luz Sanches", "Loftus Cheek",
"Lumeka", "Mousset", "Musonda", "Palmer", "Riedwald", "Sabiri",
"Vlasic", "Walker-Peters"), class = "factor"), pos = structure(c(6L,
7L, 6L, 6L, 6L, 5L, 2L, 4L, 3L, 6L, 1L, 1L, 5L, 4L, 6L, 4L,
7L, 1L, 4L, 5L), .Label = c("2. CB", "3. LB", "3. RB", "4. CM",
"5. AM", "5. WM", "6. CF"), class = "factor"), mins = c(11,
24, 18, 1, 25, 10, 90, 6, 90, 20, 99, 180, 97, 127, 35, 156,
32, 162, 258, 124)), .Names = c("team", "role", "name", "pos", "mins"), row.names = 471:490, class = "data.frame")
Here is the code I am using:
library(ggplot2)
ggplot()+
geom_dotplot(data=u21, aes(x=team, y=pos, fill=mins), binaxis='y', stackdir="center", stackratio = 1, dotsize = 0.1, binwidth=0.75, position=position_nudge(y=-0.1)) +
scale_fill_gradient(low="pink",high='red')
In my actual code I then run the ggplot line again, but calling a different data frame, with a different colour gradient, and a different nudge so the dots don't overlap.
Basically what's happening is those "grouped" dots are being treated as NA values because ggplot is receiving two min values for the same x,y coordinates, which is breaking the coloring mechanism. For example, at the intersect of "team=Chelsea" and "pos=5. WM", there are two mins: 18 and 1. The following code/graph changes NA values from the default of grey to yellow to show what's happening:
ggplot()+
geom_dotplot(data=df, aes(x=team, y=pos, fill=mins),
binaxis='y', stackdir="center",
stackratio = 1, dotsize = 0.2, binwidth=0.75,
position=position_nudge(y=-0.1)) +
scale_fill_gradient(low="pink",high='red',na.value="yellow") +
theme(axis.text.x = element_text(angle=90, vjust=0.2, hjust=1, size=8))
Output:
This was a creative test of geom_dotplot. It's not that you can't do what you're asking for with that method, but it will be overly complicated to get the effect that you want with that approach. Instead, you might have more luck with geom_jitter, which was designed to handle plotting this type of data.
ggplot(df)+
geom_jitter(aes(x=team, y=pos, col=mins),width = 0.2, height = 0) +
scale_color_gradient(low="pink",high='red',na.value="yellow") +
theme(axis.text.x = element_text(angle=90, vjust=0.2, hjust=1, size=8))
Output:
EDIT:
If you still want the complicated version with dotplot, avoiding jitter, then here's that too:
cols <- colorRampPalette(c("pink","red"))
df$cols <- cols(
max(df$mins,na.rm=T))[findInterval(df$mins,sort(1:max(df$mins,na.rm=T)))]
ggplot()+
geom_dotplot(data=df, aes(x=team, y=pos, col=mins, fill=cols),
binaxis='y',stackdir="centerwhole",stackgroups=TRUE,
binpositions="all",stackratio=1,dotsize=0.2,binwidth=0.75,
position=position_nudge(y=-0.1)) +
scale_color_gradient(low="pink",high='red',na.value="yellow") +
scale_fill_identity() +
theme(axis.text.x = element_text(angle=90, vjust=0.2, hjust=1, size=8))
Output:
For those less familiar with what's going on in the code for the third graph: step 1 is to store a gradient range with colorRampPalette; step 2 carefully assigns a hexadecimal color value to each row according to the row's df$mins value; step 3 plots the data using both color and fill arguments set so that a legend appears, yet the otherwise grey (or yellow) grouped dots are overlaid by the correct manual gradient color we've set by calling scale_fill_identity(). With this configuration, you get the right color and the right legend.
Thanks to combine stacked bars and dodged bars, I created the plot below using the data frame shown. But now, since the axis titles name the bars, how can I remove the legend elements other than for the one stacked bar? That is, can the legend show only the segments of the Big8 bar?
> dput(combo)
structure(list(firm = structure(c(12L, 1L, 11L, 13L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("Avg.", "Co", "Firm1",
"Firm2", "Firm3", "Firm4", "Firm5", "Firm6", "Firm7", "Firm8",
"Median", "Q1", "Q3"), class = "factor"), metric = structure(c(5L,
1L, 4L, 6L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Avg.",
"Big8", "Co", "Median", "Q1", "Q3"), class = "factor"), value = c(0.0012,
0.0065, 0.002, 0.0036, 0.0065, 0.000847004466666667, 0.000658907411111111,
0.0002466389, 8.41422555555556e-05, 8.19149222222222e-05, 7.97185555555556e-05,
7.82742555555556e-05, 7.56679888888889e-05), grp = structure(c(1L,
2L, 3L, 6L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Q1",
"Avg.", "Median", "Co", "Big8", "Q3"), class = "factor")), .Names = c("firm",
"metric", "value", "grp"), row.names = c(NA, -13L), class = "data.frame")
Here is the plotting code.
ggplot(combo, aes(x=grp, y=value, fill=firm)) +
geom_bar(stat="identity") +
labs(x = "", y = "") +
theme(legend.position = "bottom") +
guides(fill = guide_legend(nrow = 2))
The plot, which ideally would have a smaller set of elements in the legend.
You can manually set the breaks for scale_fill_discrete:
library(ggplot2)
ggplot(combo, aes(x=grp, y=value, fill=firm)) +
geom_bar(stat="identity") +
labs(x = "", y = "") +
theme(legend.position = "bottom") +
guides(fill = guide_legend(nrow = 2)) +
scale_fill_discrete(breaks = combo$firm[combo$metric=="Big8"])
I'm not 100% sure which labels you want to keep, but a manually entered vector, combo$firm and combo$metric will all work.
I can plot data for a particular state in the whole US map. But I want to plot only the state map with the data (Oklahoma).
How can I do it in R?
ggplot() +
geom_polygon( data=all_states, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
geom_point(data=stations,aes(x=long,y=lat),,colour="red",)+
ggtitle("Distribution of Flash Flood Events in CONUS")+
xlab('Longitude')+
ylab('Latitude')
dput(stations)
structure(list(coop = c(340017L, 340179L, 340256L, 340292L, 340548L,
340593L, 340908L, 341243L, 341504L, 341724L, 341828L, 342678L,
342912L, 342944L, 343497L, 343628L, 343821L, 343871L, 344055L,
344204L, 344235L, 344298L, 344573L, 344766L, 344861L, 345063L,
345509L, 345779L, 345855L, 346130L, 346139L, 346278L, 346629L,
346638L, 346670L, 346926L, 346935L, 347012L, 347254L, 348501L,
348677L, 349395L, 349422L, 349445L), lat = c(34.7864, 34.5903,
34.2208, 34.1714, 36.7683, 36.8125, 36.7236, 36.8003, 35.1756,
36.7747, 36.3225, 34.0003, 36.4194, 35.2164, 35.6267, 36.5914,
35.8161, 35.585, 36.0942, 34.9894, 35.0567, 36.8589, 36.7222,
36.9031, 35.8583, 34.6097, 34.8911, 35.505, 36.8833, 35.7781,
36.2283, 36.8914, 36.1217, 35.4253, 35.6239, 34.7253, 36.6692,
36.2886, 35.0539, 36.1175, 35.9369, 34.1747, 35.52, 35.4814),
long = c(-96.685, -99.3344, -95.615, -97.1294, -96.0261,
-100.5308, -102.4806, -99.6403, -98.5794, -98.3583, -95.5808,
-96.3686, -97.8747, -99.8628, -98.3225, -101.6181, -97.395,
-99.3953, -97.835, -99.0525, -96.3861, -101.2172, -97.7903,
-102.965, -97.9294, -98.4572, -99.5017, -96.9767, -94.8833,
-95.3339, -99.17, -97.0586, -98.315, -96.3033, -96.025, -97.2814,
-96.3472, -97.2897, -94.6264, -97.095, -94.9644, -97.9964,
-98.6986, -95.2039), elev = c(309.4, 420.6, 143.3, 268.2,
217.9, 751.3, 1259.7, 588.3, 451.4, 359.7, 179.2, 182.9,
379.5, 627.9, 487.7, 1008.9, 338.3, 554.7, 357.8, 474.3,
260.6, 912.9, 318.5, 1325.9, 320, 350.5, 486.2, 281.9, 245.4,
157.9, 576.1, 347.5, 370.3, 285, 197.2, 286.5, 254.5, 312.4,
134.1, 272.8, 259.1, 278, 493.2, 167.6), state = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "OK", class = "factor"),
name = structure(1:44, .Label = c("ADA", "ALTUS IRIG RSCH STN",
"ANTLERS", "ARDMORE", "BARTLESVILLE MUNI AP", "BEAVER", "BOISE CITY 2 E",
"BUFFALO 2 SSW", "CARNEGIE 5 NE", "CHEROKEE", "CLAREMORE 2 ENE",
"DURANT", "ENID", "ERICK", "GEARY", "GOODWELL RSCH STN",
"GUTHRIE 5S", "HAMMON 3 SSW", "HENNESSEY 4 ESE", "HOBART MUNI AP",
"HOLDENVILLE 2SSE", "HOOKER", "JEFFERSON", "KENTON", "KINGFISHER",
"LAWTON", "MANGUM", "MEEKER 5 W", "MIAMI", "MUSKOGEE", "MUTUAL",
"NEWKIRK 1NW", "OKEENE", "OKEMAH", "OKMULGEE WTR WKS", "PAULS VALLEY 4 WSW",
"PAWHUSKA", "PERRY", "POTEAU WTR WKS", "STILLWATER 2 W",
"TAHLEQUAH", "WAURIKA", "WEATHERFORD", "WEBBERS FALLS 5 WSW"
), class = "factor")), .Names = c("coop", "lat", "long",
"elev", "state", "name"), class = "data.frame", row.names = c(NA,
-44L))
You can use the maps package. There are a lot of simple options for the appearance of the map. Here are two very basic ones. As far as this problem goes, the maps package doesn't differ from ggplot2 very much at all, other than the fact that it's easier to code in maps.
> library(maps)
> par(mfrow = c(1, 2))
> map('state', region = 'Oklahoma')
> map('county', region = 'Oklahoma')
ADD:
Since you added the data, here is an exaggerated plot which assumes the data is called dat.
> map('county', region = 'Oklahoma')
> with(dat, points(lat ~ long, pch = 19, col = 'red'))
Edit. update according to comment
Since Geekuna is using ggplot, I am giving an answer for ggplot
m = map_data('state', region = 'Oklahoma')
ggplot() +
geom_polygon( data=m, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
geom_point(data=stations,aes(x=long,y=lat),,colour="red",)+
geom_text(data=stations, aes(x=long, y=lat,label=name), size=2, hjust=-0.1) +
ggtitle("Distribution of Flash Flood Events in CONUS")+
xlab('Longitude')+
ylab('Latitude')+
coord_fixed()
You could probably use ggcounty package if you want to show county for state as well.
library(devtools)
install_github(repo="hrbrmstr/ggcounty")
library(ggcounty)
ok <- ggcounty("Oklahoma")
ok$gg
Edited after dput from OP
ok$gg +
geom_point(data=stations,aes(x=long,y=lat),,colour="red",size=5)+
ggtitle("Distribution of Flash Flood Events in CONUS")+
xlab('Longitude')+
ylab('Latitude')
The output is as follows:
You may want to remove xlab and ylab. I hope it helped you.
Hopefully someone here will be able to help me with a problem that I'm having with a ggplot script I'm trying to get right. The script will be used many times with different data, so it needs to be relatively flexible. I've got it almost where I want it, but I've come across a problem I haven't been able to solve.
The script is for a line graph with labels for each line in the right hand margin. Sometimes the graph is faceted, other times it is not.
The piece I'm having trouble with is that I would like to color code the labels in the right margin as black if there was no significant change over time, green if there was positive change, and red if there was negative change. I've got a script that works to carry this out when I only have a single facet, but as soon as I have multiple facets in the graph, the color coding of the labels gives the following error
Error: Incompatible lengths for set aesthetics:
Below is the script with data with multiple facets. The problem seems to be in the way that I'm specifying color in the geom_text line. If I delete the color call in the geom_text line in the script, then I get the attributes printed in the correct place, just not colored. I'm really at a loss on this one. This is my first post here, so let me know if I've done anything wrong with my post.
WITH MULTIPLE FACETS (DOES NOT WORK)
require(ggplot2)
require(grid)
require(zoo)
require(reshape)
require(reshape2)
require(directlabels)
time.data<-structure(list(Attribute = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L, 5L, 5L, 6L, 6L), .Label = c("Taste 1", "Taste 2", "Taste 3",
"Use 1", "Use 2", "Use 3"), class = "factor"), Attribute.Category = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Nutritional/Usage",
"Taste/Quality"), class = "factor"), Attribute.Order = c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), Category.Order = c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), Color = structure(c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), .Label = c("#084594",
"#2171B5", "#4292C6", "#6A51A3", "#807DBA", "#9E9AC8"), class = "factor"),
value = c(75L, 78L, 90L, 95L, 82L, 80L, 43L, 40L, 25L, 31L,
84L, 84L), Date2 = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L), .Label = c("1/1/2013", "9/1/2012"), class = "factor")), .Names = c("Attribute",
"Attribute.Category", "Attribute.Order", "Category.Order", "Color",
"value", "Date2"), class = "data.frame", row.names = c(NA, -12L
))
label.data<-structure(list(7:12, Attribute = structure(1:6, .Label = c("Taste 1",
"Taste 2", "Taste 3", "Use 1", "Use 2", "Use 3"), class = "factor"),
Attribute.Category = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("Nutritional/Usage",
"Taste/Quality"), class = "factor"), Attribute.Order = 1:6,
Category.Order = c(1L, 1L, 1L, 2L, 2L, 2L), Color = structure(1:6, .Label = c("#084594",
"#2171B5", "#4292C6", "#6A51A3", "#807DBA", "#9E9AC8"), class = "factor"),
Significance = structure(c(2L, 3L, 1L, 1L, 3L, 2L), .Label = c("neg",
"neu", "pos"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = "1/1/2013", class = "factor"),
value = c(78L, 95L, 80L, 40L, 31L, 84L), Date2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = "2013-01-01", class = "factor"),
label.color = structure(c(1L, 2L, 3L, 3L, 2L, 1L), .Label = c("black",
"forestgreen", "red"), class = "factor")), .Names = c("",
"Attribute", "Attribute.Category", "Attribute.Order", "Category.Order",
"Color", "Significance", "variable", "value", "Date2", "label.color"
), class = "data.frame", row.names = c(NA, -6L))
color.palette<-as.character(unique(time.data$Color))
time.data$Date2<-as.Date(time.data$Date2,format="%m/%d/%Y")
plot<-ggplot()+
geom_line(data=time.data,aes(as.numeric(time.data$Date2),time.data$value,group=time.data$Attribute,color=time.data$Color),size=1)+
geom_text(data=label.data,aes(x=Inf, y=label.data$value, label=paste(" ",label.data$Attribute)),
color=label.data$label.color,
size=4,vjust=0, hjust=0,na.rm=T)+
facet_grid(Attribute.Category~.,space="free")+
theme_bw()+
scale_x_continuous(breaks=as.numeric(unique(time.data$Date2)),labels=format(unique(time.data$Date2),format = "%b %Y"))+
theme(strip.background=element_blank(),
strip.text.y=element_blank(),
legend.text=element_blank(),
legend.title=element_blank(),
plot.margin=unit(c(1,5,1,1),"cm"),
legend.position="none")+
scale_colour_manual(values=color.palette)
gt3 <- ggplot_gtable(ggplot_build(plot))
gt3$layout$clip[gt3$layout$name == "panel"] <- "off"
grid.draw(gt3)
Some problems:
Inside your aesthetic declarations, you should not be referencing the data columns as time.data$Date2, but just as Date2. The data argument specifies where to look for that information (which needs to all be in the same data.frame for a given layer, but, as you take advantage of, can vary layer to layer).
In the geom_text call, color was not inside the aes call; if you are mapping it to data which is in the data.frame, you have to have it inside the aes call. This would throw a different error after fixing the first part because then it would not be able to find label.color anywhere because it would not know to look inside label.data.
Fixing those, then the scale_colour_manual complains that there are 9 colors and you have only supplied 6. That is because there are 6 colors from the lines and 3 from the text. Since you specified these as actual color names, you can just use scale_colour_identity.
Putting this all together:
plot <- ggplot()+
geom_line(data=time.data, aes(as.numeric(Date2), value,
group=Attribute, color=Color),
size=1)+
geom_text(data=label.data, aes(x=Inf, y=value,
label=paste(" ",Attribute),
color=label.color),
size=4,vjust=0, hjust=0)+
facet_grid(Attribute.Category~.,space="free") +
scale_x_continuous(breaks=as.numeric(unique(time.data$Date2)),
labels=format(unique(time.data$Date2),format = "%b %Y")) +
scale_colour_identity() +
theme_bw()+
theme(strip.background=element_blank(),
strip.text.y=element_blank(),
legend.text=element_blank(),
legend.title=element_blank(),
plot.margin=unit(c(1,5,1,1),"cm"),
legend.position="none")
gt3 <- ggplot_gtable(ggplot_build(plot))
gt3$layout$clip[gt3$layout$name == "panel"] <- "off"
grid.draw(gt3)
To get an idea how much you can strip down your example, this is much closer to minimal:
time.data <-
structure(list(Attribute = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L), .Label = c("Taste 1", "Taste 2", "Use 1", "Use 2"), class = "factor"),
Attribute.Category = structure(c(2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L), .Label = c("Nutritional/Usage", "Taste/Quality"), class = "factor"),
Color = c("#084594", "#084594", "#2171B5", "#2171B5", "#6A51A3",
"#6A51A3", "#807DBA", "#807DBA"), value = c(75L, 78L, 90L,
95L, 43L, 40L, 25L, 31L), Date2 = structure(c(15584, 15706,
15584, 15706, 15584, 15706, 15584, 15706), class = "Date")), .Names = c("Attribute",
"Attribute.Category", "Color", "value", "Date2"), row.names = c(NA,
-8L), class = "data.frame")
label.data <-
structure(list(value = c(78L, 95L, 40L, 31L), Attribute = structure(1:4, .Label = c("Taste 1",
"Taste 2", "Use 1", "Use 2"), class = "factor"), label.color = c("black",
"forestgreen", "red", "forestgreen"), Attribute.Category = structure(c(2L,
2L, 1L, 1L), .Label = c("Nutritional/Usage", "Taste/Quality"), class = "factor"),
Date2 = structure(c(15706, 15706, 15706, 15706), class = "Date")), .Names = c("value",
"Attribute", "label.color", "Attribute.Category", "Date2"), row.names = c(NA,
-4L), class = "data.frame")
ggplot() +
geom_line(data = time.data,
aes(x=Date2, y=value, group=Attribute, colour=Color)) +
geom_text(data = label.data,
aes(x=Date2, y=value, label=Attribute, colour=label.color),
hjust = 1) +
facet_grid(Attribute.Category~.) +
scale_colour_identity()
The theme stuff (and the making the labels visible outside the plot) isn't relevant to the question, nor is the x-axis conversions from Date to numeric to handle having Inf. I also trimmed the data to just the needed columns, and reduced categorical variable to only two categories.