To my geom_tile plot I need columns names, here 6, above the first row and below the subtitle
I tried adding it by
geom_text(x=10, y=10, label="testTEXT") +
but it doesn't show. Perhaps the coordinates are wrong (I tested various value)?
Here the full code:
library(tidyverse)
s1 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s2 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s3 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s4 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s5 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s6 = sample(c("p1","p12","p3","p14","p5","p13"),6)
groupsNo = 6
BLUB = c(s1,s2,s3,s4,s5,s6)
df <- cbind.data.frame(items = BLUB,
x = rep((1:groupsNo),each = length(s1)),
y = rep(1:length(s1), groupsNo),
color = BLUB)
p1 <- ggplot(df,aes(x = x, y = y,fill=factor(color))) +
geom_line(aes(group = items,color=factor(color)), size = 2) +
geom_tile(width = 0.6, height = 0.6, color = 'black') +
theme_void() +
geom_text(aes(label = items), size=5) +
# geom_text(x=100, y=0, label="testTEXT") +
theme(legend.position = "none") +
labs(
y = "", x = "",
title = "Plot for random data",
subtitle = "text",
caption = "text")
print(p1)
The basic problem with your plot is the use of theme_void() which blanks almost all plot labels. You have to override this by specifying the theme for axis text and then blanking the y axis.
You can position the x axis labels at the top by specifying position = "top" in the scale_x_continuous function. You'll also need to explicitly specify the breaks to have labels for all columns. The revised code would be:
p1 <- ggplot(df,aes(x = x, y = y, fill=factor(color))) +
geom_line(aes(group = items,color=factor(color)), size = 2) +
geom_tile(width = 0.6, height = 0.6, color = 'black') +
theme_void() +
theme(axis.text = element_text(size = 12, color = "black",
inherit.blank = FALSE))+
theme(axis.text.y = element_blank()) +
scale_x_continuous(position = "top", breaks = df$x ) +
#
# If x were a character value rather than a numeric one,
# replace scale_x_continuous(..) with
# scale_x_discrete( position = "top") +
#
geom_text(aes(label = items), size=5) +
theme(legend.position = "none") +
labs(
y = "", x = "",
title = "Plot for random data",
subtitle = "text",
caption = "text")
print(p1)
Try this:
library(tidyverse)
s1 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s2 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s3 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s4 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s5 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s6 = sample(c("p1","p12","p3","p14","p5","p13"),6)
groupsNo = 6
BLUB = c(s1,s2,s3,s4,s5,s6)
df <- cbind.data.frame(items = BLUB,
x = rep((1:groupsNo),each = length(s1)),
y = rep(1:length(s1), groupsNo),
color = BLUB)
ggplot(df,aes(x = x, y = y,fill=factor(color))) +
geom_line(aes(group = items,color=factor(color)), size = 2) +
geom_tile(width = 0.6, height = 0.6, color = 'black') +
theme_void() +
geom_text(aes(label = items), size=5) +
annotate(geom="text", x=unique(df$x), y=max(df$y)+0.5, label="testTEXT") +
theme(legend.position = "none") +
labs(
y = "", x = "",
title = "Plot for random data",
subtitle = "text",
caption = "text")
Related
For the example below, I wonder to know how can I have the labels sorted alphabetically when they appear on the y bar.
Thanks,
NM
# Create labels for plot
boxLabels = c("Package recommendation", "Breeder’s recommendations", "Vet’s
recommendation", "Measuring cup", "Weigh on scales", "Certain number of
cans", "Ad lib feeding", "Adjusted for body weight")
df <- data.frame(yAxis = length(boxLabels):1,
boxOdds =
c(2.23189,1.315737,1.22866,.8197413,.9802449,.9786673,.6559005,.5929812),
boxCILow =
c(.7543566,1.016,.9674772,.6463458,.9643047,.864922,.4965308,.3572142),
boxCIHigh =
c(6.603418,1.703902,1.560353,1.039654,.9964486,1.107371,.8664225,.9843584)
)
(p <- ggplot(df, aes(x = boxOdds, y = boxLabels)) +
geom_vline(aes(xintercept = 1), size = .25, linetype = 'dashed') +
geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height =
.2, color = 'gray50') +
geom_point(size = 3.5, color = 'orange') +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
scale_x_continuous(breaks = seq(0,7,1) ) +
coord_trans(x = 'log10') +
ylab('') +
xlab('Odds ratio (log scale)') +
annotate(geom = 'text', y =1.1, x = 3.5, label ='Model p < 0.001\nPseudo
R^2 = 0.10', size = 3.5, hjust = 0) + ggtitle('Intention to remove box
turtles from the road')
)
the values are shown in alphabetical order but from bottom to top. to reverse, you could try using fct_rev from the forcats package:
replace y = boxLabels with
y = forcats::fct_rev(boxLabels)
I have a plot like this:
library("ggplot2")
library("ggtext")
df = data.frame(x = rnorm(n=12), y = rnorm(n=12),
groupshape = rep(c("a","b","c"), 4),
groupcol = rep(c("e","d","f"), 4))
p = ggplot(df, aes(x = x,y = y,
shape = groupshape,
color = groupcol)) +
geom_point() + theme_classic() + labs(shape = "shape", color = "color")
Producing a plot like this:
But I want to know how to get something like this with the legend:
How would I do that? I can get rid of the shape itself through guides by turning them all while, but then the text just hovers menacingly without alignment to the legend below.
This could be achieved via the label.position and the label.theme arguments of guide_legend:
library("ggplot2")
library("ggtext")
df = data.frame(x = rnorm(n=12), y = rnorm(n=12),
groupshape = rep(c("a","b","c"), 4),
groupcol = rep(c("e","d","f"), 4))
ggplot(df, aes(x = x,y = y,
shape = groupshape,
color = groupcol)) +
geom_point() + theme_classic() + labs(shape = "shape", color = "color") +
guides(color = guide_legend(label.position = "left", label.theme = element_text(face = "bold")),
shape = guide_legend(label.position = "left"))
I have two plots I just want to know how I can add a legend for the blue and gray bar charts and also could you please show me how you could also edit the legend tittle.
X1 <- c(seq(7.912087912,44.83516484,1.538461538))
X2 <- c(seq(7.912087912,49.45054945,1.538461538))
dat2 <- data.frame(x = X2 , y = rnorm(28, 26, 5))
dat1 <- data.frame(x = X1 , y = rnorm(100, 25, 4))
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y), stat = "identity",alpha = 0.3, position = "stack" ) + labs( x = " Time [ S ]", y = "Frequency") + theme_minimal() +
ggtitle("Histogram Of Time In Tank") + theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y ), stat = "identity", alpha = .3, position = "stack", fill='lightblue' , color='lightblue4')
+ scale_linetype_discrete(name = LegendTitle)
If you want a legend in ggplot, you need to have an aesthetic mapping inside your aes() or no legend will appear. Here's how we can set a mapping and then use the scale to set the colors we want
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y, fill="Grey Bars"), stat = "identity",alpha = 0.3, position = "stack" ) +
labs( x = " Time [ S ]", y = "Frequency") +
theme_minimal() +
ggtitle("Histogram Of Time In Tank") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y, fill='Blue Bars') , stat = "identity", alpha = .3, position = "stack", color='lightblue4') +
scale_fill_manual(name="Bars", values=c("Grey Bars" = "grey35", "Blue Bars" = "lightblue"))
I have the following dataframe in R
DF_1<-data.frame("ID"=c("A_1", "A_2"), 'Sum'= c(2500,12500), "RR"=
c(95,95), "CC"= c(50,50), "nn"=c(4,4), "DP"= c(12.5,100))
I have created the following barplot with text written in the barplot as follows
p2<-ggplot(data = DF_1, mapping = aes(x = ID, y = DP,
fill=str_wrap(ID,10))) + geom_bar(stat = 'identity', width = .35, position
= "dodge")+ geom_text(aes(label=RR) , position = position_dodge(0.9),vjust
= 2,check_overlap = TRUE)+geom_text(aes(label=CC) , position = postion_dodge(0.9),vjust = 4,check_overlap = TRUE)+ geom_text(aes(label=nn) , position = position_dodge(0.9),vjust = 6,check_overlap = TRUE)+ labs(fill = "LEGEND")+labs(x = "XLabels", y= "DPP")+theme(legend.key.height = unit( 2 ,"cm"))+theme(axis.text.x=element_blank())+ggtitle("DPPCHART")+ theme(plot.title = element_text(hjust = 0.5))
In the plot so generated the three text labels are generated clearly on the barplot. The next step is to make dynamic ticks work
p<-ggplotly(p, dynamicticks=T)
Now the three text labels overlap each other.
Is there a way to retain the text labels separately when using ggplotly.
You can use HTML directly inside the labels like this to remove the overlap:
library(stringr)
library(ggplot2)
library(plotly)
DF_1 <- data.frame("ID"=c("A_1", "A_2"), 'Sum'= c(2500,12500),
"RR"= c(95,95), "CC"= c(50,50), "nn"=c(4,4), "DP"= c(12.5,100))
p <- ggplot(data = DF_1, mapping = aes(x = ID, y = DP,fill=str_wrap(ID,10))) +
geom_bar(stat = 'identity', width = .35, position = "dodge") +
geom_text(aes(label=paste0(RR, "<br>", CC, "<br>", nn)) ,
position = position_dodge(0.9), vjust = 2, check_overlap = TRUE) +
labs(fill = "LEGEND") + labs(x = "XLabels", y= "DPP") +
theme(legend.key.height = unit( 2 ,"cm")) +
theme(axis.text.x=element_blank()) +
ggtitle("DPPCHART") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_y_continuous(limits = c(0,110))
ggplotly(p) # can use with or without dynamicTicks=TRUE
Output is:
df.test <- data.frame(val=c(0.55,0.42,-0.05),name=letters[1:3],
desc='This is the description of values'
p <- ggplot(df.test, aes(name, val, label = desc)) +
geom_bar(stat = "identity", col = 'black', fill = 'lightgreen') +
labs(title = "Test", x = " ", y = "value", fill = "") +
theme_bw() +
guides(fill = FALSE)
p + geom_text(angle = 90, size = 8, hjust = 1.25, position = position_dodge(width = 0.9))
This generates the following plot:
I want to align the text and force the it to start at the beginning of each chart, so that all of them can be visible (it is ok if it falls outside the small chart). How can I achieve this?
Is this what you're looking for?
p <- ggplot(df.test,aes(name,val,label=desc))+
geom_bar(stat="identity",col='black',fill='lightgreen')+
labs(title = "Test", x = " ", y = "value",fill = "")+
theme_bw()+
guides(fill=FALSE)
p+geom_text(angle=90,size=8,hjust=0,aes(x=name,y=rep(0,nrow(df.test)),label=desc),inherit.aes=F)