given the following reproducible example
ggplot(diamonds, aes(cut, price)) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=5, size=2, colour='black')+
coord_trans(y="log10")
ggplot(diamonds, aes(cut, price)) +
geom_boxplot() +
coord_trans(x="log10")+
stat_summary(fun.y=mean, geom="point", shape=5, size=2, colour='black')+
coord_flip()
it's not clear to me how to transform the coordinates so that to keep the log transformation of the 'price' axis (y flipped);
in fact, after the coordinates flipping the price axis is apparentely loosing the log transformation as a (unwanted by me) side effect;
to be noted that I need that the transforming of the coordinate system occurs after the statistic has been computed, and this is why I used 'coord_trans()' and not 'scale_y_log10()'...
any help for pointing me in the right direction?
thank you
...oops, very simply switch the aes mappings, drop the flip of coordinates and it's done!
ggplot(diamonds, aes(price, cut)) +
geom_boxplot() +
stat_summary(fun=mean, geom="point", shape=5, size=2, colour='black')+
coord_trans(x="log10")
Related
I would like to include a legend inside the top right of my plot, indicating the parameter values of the plots.
ggplot() +
geom_line(data=data1, aes(x=x, y=y1), color='green', linetype = "twodash", size=0.5) +
geom_line(data=data2, aes(x=x, y=y2), color='red', linetype="longdash", size=0.5) +
geom_line(data=data3, aes(x=x, y=y3), color='blue') +
theme_classic() +
labs(x='input, x',
y='output, f(x)')
Can someone please say how this is done. Thanks.
I think this is a very good source of "what to do with ggplot"
http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#changing-the-position-of-the-legend
from there
# Position legend in graph, where x,y is 0,0 (bottom left) to 1,1 (top right)
bp + theme(legend.position=c(1, 1))
should/could do it?
I have the following code:
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
stat_summary(fun.y=mean, geom='point', size=2, fill='white')
I know there are probably other ways of plotting this mean using the iris data. For my own data, though, it is the only way.
PROBLEM: the code above doesn't give white-filled points, but solid black points. Is there ar way to set the fill-colour when using the stat_summary argument?
Thanks!
Either use color instead of fill
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
stat_summary(fun.y=mean, geom='point', size=2, color='white')
or use a symbol shape that has a fill and a border color
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
stat_summary(fun.y=mean, geom='point', size=2, shape=21, fill="blue", color="red")
I used the following script to make the graph below
hypttauplot <- qplot(fuclin_csf, fitHYPTTAU, data=selTAU, geom=c("smooth"),
method="glm", color='black', linetype=BL_HYPT) +
theme_classic() + xlab("Time (years)") + ylab("Tau (pg/ml)") +
scale_x_continuous(expand=c(0,0)) +
ggtitle("A. Hypertension") +
theme(legend.position = "none")
But now my questions is: why are the lines red instead of black? And how can I change them to black?
You have to use I() to set the aesthetics manually in qplot(), e.g. colour=I("black") (setting vs. mapping of aesthetics).
# mapping
qplot(carat, price, data=diamonds, color="black")
# equivalent to ggplot(data=diamonds, aes(carat, price, color="black")) + geom_point()
# setting
qplot(carat, price, data=diamonds, color=I("black"))
# equivalent to ggplot(data=diamonds, aes(carat, price), color="black") + geom_point()
You can use scale_colour_manual to specify an own set of mappings.
I'm working on some flattening of overlapping ranges and would like to visualize the initial data (overlapping) and the resulting set (flattened) the following way:
Initial data:
Resulting set:
Is such possible with R and, for example, ggplot2?
read.table(header=TRUE, sep=",", text="color,start,end
red,12.5,13.8
blue,0.0,5.4
green,2.0,12.0
yellow,3.5,6.7
orange,6.7,10.0", stringsAsFactors=FALSE) -> df
library(ggplot2)
df$color <- factor(df$color, levels=rev(df$color))
ggplot(df) +
geom_segment(aes(x=start, xend=end, y=color, yend=color, color=color), size=10) +
scale_x_continuous(expand=c(0,0)) +
scale_color_identity() +
labs(x=NULL, y=NULL) +
theme_minimal() +
theme(panel.grid=element_blank()) +
theme(axis.text.x=element_blank()) +
theme(plot.margin=margin(30,30,30,30))
There are other posts on SO that show how to get the y labels like you have shown (we can't do all the work for you ;-)
The answer to the second part of the question can be using #hrbrmstr 's great answer for the first part. We can use overplotting to our advantage and simply set the y coordinates for the segments to a fixed value (for example 1, which where "red" is):
p <- ggplot(df) +
geom_segment(aes(x=start, xend=end, color=color),
y=1, yend=1, size=10) +
scale_x_continuous(expand=c(0,0)) + scale_color_identity() +
labs(x=NULL, y=NULL) +
theme_minimal() +theme(panel.grid=element_blank()) +
theme(axis.text.x=element_blank()) +
theme(plot.margin=margin(30,30,30,30))
print(p)
data=data.frame(x=rep(0:9, each=2))
ggplot(data, aes(x=factor(x))) + geom_bar(alpha=0.5) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=factor(x), y=y), alpha=0.5)
ggplot(data, aes(x=factor(x))) + geom_bar(alpha=0.5) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=factor(x), y=y), alpha=0.5) +
scale_x_discrete(limits=0:10)
Also, do I have to factor given x is integer so it is discrete already?
Wrong order
Wrong x axis label.
ggplot(data, aes(x=x)) + geom_bar(alpha=0.5) + scale_x_discrete(limits=0:10) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=x, y=y), alpha=0.5)
You can force a discrete scale to get what you want. It is odd how when you mix geom_point() and geom_bar() ggplot starts ordering things in unexpected ways.