I want make curved text around ggplot with coord_polar. I have data.frame:
z <- data.frame( a=c("sensor 1","sensor 2","sensor 3","sensor 4","sensor 5","sensor 6","sensor 7","sensor 8"), b=c(50, 60, 70, 20,90,110,30,100))
And here is code where I create my ggplot:
cxc <- ggplot(z, aes(x=a, y=b, fill=factor(b))) +
geom_bar(width = 1,stat="identity",colour = "black")
cxc + coord_polar() +
theme_linedraw() +theme(axis.ticks =element_blank(), axis.text.y =element_blank(), axis.title=element_blank(), axis.text.x=element_text(size = 12,angle = 45))
Here is image from my result. I want making texts (x axis): sensor 1, sensor 2... making curved like I draw with red color around circle from coord_polar. Must fit with circle.
I am not sure on curving the text, but if you want them to at least rotate, this is a start:
myAng <-
seq(-20,-340,length.out = 8)
cxc + coord_polar() +
theme_linedraw() +
theme(axis.ticks =element_blank()
, axis.text.y =element_blank()
, axis.title=element_blank()
, axis.text.x=element_text(size = 12
,angle = myAng))
I will add in my hesitation on using such angles (or even polar coordinates for a plot like this). However, I assume that you have a good reason for doing it, and that this MWE just doesn't show it.
The {geomtextpath} package has a dedicated function for exactly this use case, including curved text. See also: here.
library(ggplot2)
library(geomtextpath) # remotes::install_github("AllanCameron/geomtextpath")
z <- data.frame(
a=c("sensor 1","sensor 2","sensor 3","sensor 4",
"sensor 5","sensor 6","sensor 7","sensor 8"),
b=c(50, 60, 70, 20,90,110,30,100)
)
cxc <- ggplot(z, aes(x=a, y=b, fill=factor(b))) +
geom_bar(width = 1,stat="identity",colour = "black")
cxc + coord_curvedpolar() +
theme_linedraw() +
theme(axis.ticks =element_blank(),
axis.text.y =element_blank(),
axis.title=element_blank(),
axis.text.x=element_text(size = 12))
Created on 2021-12-08 by the reprex package (v2.0.1)
Disclaimer: I'm a co-author of that package.
I don't know of a way to make them curve exactly like that using ggplot2 but we can at least remove the funny angle...
pm <- grid::unit(c(2, 2, 2, 2), "cm")
RadarTheme<-theme(panel.background=element_blank(),
plot.title= element_text(size = 25,face=c("bold","italic")),
plot.margin = pm,
text=element_text(family="Open Sans"), aspect.ratio = 1,
legend.position="bottom",legend.title=element_blank(),legend.direction="vertical",
strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = 15,face ="bold"),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.line.x=element_line(size=0.5),
panel.grid.major=element_line(size=0.3,linetype = 2,colour="grey"))
cxc <- ggplot(z, aes(x=a, y=b, fill=factor(b))) +
geom_bar(width = 1,stat="identity",colour = "black")
cxc + coord_polar() + RadarTheme
Related
I little experience with ggplot2. I am trying to plot coverage probability and cohort size using the code below:
library("reshape2")
library("ggplot2")
library(latex2exp)
CP1 <-c(0.953,0.942,0.947,0.958)
CP2 <- c(0.937,0.952,0.955,0.957)
cohort <- c(500,1000,5000,10000)
mdata <- data.frame(rate1=CP1,rate2=CP2,cohort.size=cohort)
mydata <- melt(mdata,id='cohort.size',value.name="CP")
ggplot(mydata , aes(x=cohort.size, y=CP)) +
geom_line(size=1,aes(colour=variable)) +
geom_point( size=4, shape=0)+ coord_cartesian(ylim = c(0,1)) +
scale_x_continuous(breaks=c(500,1000,5000,10000))+
scale_color_discrete(labels = unname(TeX(c(" $\\r_1$", "$\\r_2$")))) +
geom_hline(yintercept =c(0.936,0.964) ,linetype="dashed") +
theme(legend.title = element_blank(), axis.title.x = element_text(color="#993333", size=14, face="bold"),
axis.title.y = element_text(color="#993333", size=14, face="bold"),
plot.title = element_text(color="#993333", size=14, face="bold"),
legend.position = c(.85, .85),
legend.justification = c("right", "top"),
legend.box.just = "right",
legend.margin = margin(6, 6, 6, 6),legend.text=element_text(size=20)) + xlab("Cohort Size") + ylab("Coverage Proability")+
annotate("text",
x = 8700,
y = 0.68,
label =expression(bold(paste("MN=57% \n AB=38% \n XYZ=5%" ))),parse = TRUE,size=5)
I have three questions:
1. When I run the code, I get a warning; how can I fix it.
2. There are two horizontal black dashed lines and I want to have just one legend for both to represent '95% CL'.
3. I feel the code is too much, is there a much simpler way of writing it using ggplot2 only.
Thanks!!
I can't install latex2exp. Without this package, you simply can try this and in my opinion all three questions are solved:
ggplot(mydata , aes(x=cohort.size, y=CP)) +
geom_line(size=1,aes(colour=variable)) +
geom_point( size=4, shape=0)+
geom_hline(data = data.frame(yintercept =c(0.936,0.964)),
aes(yintercept =yintercept, linetype ='95% CL')) +
scale_linetype_manual("", values = 2) +
ylim(0,1) +
annotate("text",
x = 8700,
y = 0.68,
label = paste("MN=57%\n AB=38%\n XYZ=5%" ),
size=5, fontface =2)
I am generating bubble charts from NBA shot data clusters. The final form of the data is:
Where Group.1 is the index of the cluster, ad.SHOT_MADE_FLAG is the field goal percent for the cluster, coords.x1 and x2 are the mean x and y coordinates of the points in that cluster, and x is the number of shots (x and y points) in that cluster.
I am plotting the data with the following:
courtImg.URL <- "https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg"
court <- rasterGrob(readJPEG(getURLContent(courtImg.URL)),
width=unit(1,"npc"), height=unit(1,"npc"))
p6 <- ggplot(final, aes(x = final$coords.x1, y = final$coords.x2, size =
final$x,fill=final$ad.SHOT_MADE_FLAG)) +
geom_point(shape = 21) +
annotation_custom(court, -250, 250, -52, 418) +
scale_x_continuous() +
coord_fixed() +
scale_fill_gradientn(colours = c("Blue","Red")) +
theme(line = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.title = element_blank(),
plot.title = element_text(size = 17, lineheight = 1.2, face = "bold")) +
ggtitle("Stephen Curry Shot Chart")
p6
This outputs the following chart
I am wanting to solve two issues with this. First the background image is covering up the majority of the data. Second, I want to only show the plot below the 418 point on the y axis. I dont want to show shots from the backcourt as they aren't as relevant. Just for reference, when I remove the annotation_custom() line, it shows the following plot:
So the implementation of the annotation_custom line appears to be part of the problem. Any help would be greatly appreciated. Thanks!
ggplot2 draws plot layers in the order you specify them. To move the image of the court below the points, put it first in the drawing order. The other fix that might make your plot a little nicer is to make the panel background transparent so that you can see the points on top of the image, which I assume is what you're going for.
You can set the ends of the plots using the limits argument in scale_y_continuous().
Updated plotting code:
p6 <- ggplot(final, aes(x = final$coords.x1, y = final$coords.x2, size =
final$x,fill=final$ad.SHOT_MADE_FLAG)) +
annotation_custom(court, -250, 250, -52, 418) +
geom_point(shape = 21) +
scale_x_continuous() +
scale_y_continuous(limits=c(-52,418)) +
coord_fixed() +
scale_fill_gradientn(colours = c("Blue","Red")) +
theme(line = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.title = element_blank(),
panel.background = element_rect(fill="transparent"),
plot.title = element_text(size = 17, lineheight = 1.2, face = "bold")) +
ggtitle("Stephen Curry Shot Chart")
p6
I am wondering how I can change the grid line on x value=0, marked with a cross in the image, in my graph to shows the change from +ive to -ive. I would like to have it marked red with the same thickness. Thank you
=== Updated based on the comment
#Mtoto: My apologies. Here is the script.
df.boxplot<- ggplot(melt(df[,c(2:7)]), aes(variable, value))
df.boxplot +
geom_boxplot(lwd=1.2)+ theme_economist() + scale_colour_economist()+
scale_y_continuous(minor_breaks=seq(-5, 10, 0.5),name="Linear Measurements (mm)", breaks=seq(-5, 10, 1)) +
theme(axis.title.x = element_text(face="bold", colour="Black", size=20),
axis.text.x = element_text(face="bold", colour="Black", vjust=0.5, size=20)) +
scale_x_discrete(name="",labels=c("T0 A","T1 B","Δ AB","T0 C","T1 D","Δ CD")) +
theme(axis.title.y = element_text(face="bold", colour="Black", size=30,margin=margin(0,20,0,0)),
axis.text.y = element_text(angle=90, vjust=1, size=20)) +
theme(panel.grid.minor = element_line(colour="White",size=0.2))+
theme(axis.ticks = element_blank())+
ggtitle(" Title")+
theme(plot.title = element_text(size=25,lineheight=2, hjust =0.5, vjust=0.5, margin = margin(20, 10, 20, 0)))
I would also like to add a gap (one x unit/level) between the first three boxplots and the second three boxplots. I tried adding a NA column and use drop=FALSE and it didn't work.
I think you want to look at geom_hline -- I'm sure this is a duplicate...
library(ggplot2)
df <- data.frame(x = gl(5, 25),
y = rnorm(125))
ggplot(df, aes(x, y)) +
geom_boxplot() +
geom_hline(aes(y_intercept = 0), color = "red")
I have a multi-panel plots that I have created using facet_wrap function and I would like to add a top tick to each of the y-axis (so that each y-axis ends with a number).
I can manually do this when I am creating one plot at a time with scale_y_continuous (setting a limit based on the maximum value), but I am not sure how I would do this using facet_wrap. Setting the limit using max() doesn't seem to work.
Just to give you an idea of what I am talking about, below is a code for creating single plot with ggplot2, and enabling the top tick on the y-axis to appear.
plot <- ggplot(diamonds, aes(clarity)) +
geom_bar() +
scale_y_continuous(expand = c(0, 0),limits=c(0,15000)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
axis.line = element_line(),
axis.title = element_text(size=15,face="bold")) +
xlab("x_1") +
ylab("y_1") +
theme(panel.background = element_blank()
,panel.grid.major = element_blank()
,panel.grid.minor = element_blank()
,panel.border = element_blank()) +
labs(title = "xy", size = 20)
plot
And this is the plot.
Thank you in advance!
Edit: I have edited the code to show the plot. Thanks again!
You can define your own function for the breaks. A simple solution would be
break_fct <- function(x){
round(seq(min(x), max(x), length = 5))
}
Of course you can probably improve on this using some combination of pretty and the maximum. You should find a solution that works well for your data.
plot <- ggplot(diamonds, aes(clarity)) +
geom_bar() +
scale_y_continuous(expand = c(0, 0), breaks = break_fct) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
axis.line = element_line(),
axis.title = element_text(size=15, face="bold")) +
xlab("x_1") +
ylab("y_1") +
theme(panel.background = element_blank()
,panel.grid.major = element_blank()
,panel.grid.minor = element_blank()
,panel.border = element_blank()) +
labs(title = "xy", size = 20) +
facet_grid(cut~., scales = "free_y")
plot
How can scatter plots with alpha transparent, scale-less histograms can be made in R, like this figure?
looks like it's not made in ggplot2.
does anyone know what command is used?
library(ggplot2)
library(gridExtra)
set.seed(42)
DF <- data.frame(x=rnorm(100,mean=c(1,5)),y=rlnorm(100,meanlog=c(8,6)),group=1:2)
p1 <- ggplot(DF,aes(x=x,y=y,colour=factor(group))) + geom_point() +
scale_x_continuous(expand=c(0.02,0)) +
scale_y_continuous(expand=c(0.02,0)) +
theme_bw() +
theme(legend.position="none",plot.margin=unit(c(0,0,0,0),"points"))
theme0 <- function(...) theme( legend.position = "none",
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin = unit(0,"null"),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.length = unit(0,"null"),
axis.ticks.margin = unit(0,"null"),
panel.border=element_rect(color=NA),...)
p2 <- ggplot(DF,aes(x=x,colour=factor(group),fill=factor(group))) +
geom_density(alpha=0.5) +
scale_x_continuous(breaks=NULL,expand=c(0.02,0)) +
scale_y_continuous(breaks=NULL,expand=c(0.02,0)) +
theme_bw() +
theme0(plot.margin = unit(c(1,0,0,2.2),"lines"))
p3 <- ggplot(DF,aes(x=y,colour=factor(group),fill=factor(group))) +
geom_density(alpha=0.5) +
coord_flip() +
scale_x_continuous(labels = NULL,breaks=NULL,expand=c(0.02,0)) +
scale_y_continuous(labels = NULL,breaks=NULL,expand=c(0.02,0)) +
theme_bw() +
theme0(plot.margin = unit(c(0,1,1.2,0),"lines"))
grid.arrange(arrangeGrob(p2,ncol=2,widths=c(3,1)),
arrangeGrob(p1,p3,ncol=2,widths=c(3,1)),
heights=c(1,3))
Edit:
I couldn't find out what causes the space below the densities geoms. You can fiddle with the plot margins to avoid it, but I don't really like that.
p2 <- ggplot(DF,aes(x=x,colour=factor(group),fill=factor(group))) +
geom_density(alpha=0.5) +
scale_x_continuous(breaks=NULL,expand=c(0.02,0)) +
scale_y_continuous(breaks=NULL,expand=c(0.00,0)) +
theme_bw() +
theme0(plot.margin = unit(c(1,0,-0.48,2.2),"lines"))
p3 <- ggplot(DF,aes(x=y,colour=factor(group),fill=factor(group))) +
geom_density(alpha=0.5) +
coord_flip() +
scale_x_continuous(labels = NULL,breaks=NULL,expand=c(0.02,0)) +
scale_y_continuous(labels = NULL,breaks=NULL,expand=c(0.00,0)) +
theme_bw() +
theme0(plot.margin = unit(c(0,1,1.2,-0.48),"lines"))
I have no idea whether there is a package that does that directly, but I'm sure this can be done in R. Transparency is easy: you add another two digits to the RGB specification of a color for a given transparency:
#FF0000 # red
#FF0000FF # full opacity
#FF000000 # full transparency
Combining different plots is also easy using the layout function. As for the vertical density plot, it is just the same as the horizontal plot with x and y switched. The example given here can easily be expanded to include colors, smaller margins etc. I can try to come up with a more elaborate example if this description is not sufficient.