ggplot 'race track' plot with polar_coord + points - r

I'm struggling to get polar_coords to work as I had hoped. I want each item to be represented by a coloured track, with a range of 1:50000. I then wanted to plot points over these tracks at the corresponding locations, with symbols representing the different categories. The points would then be annotated with the id.
Dataframe:
structure(list(item = structure(c(1L, 2L, 2L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L), .Label = c("AA", "AB", "AC", "AD", "AE",
"BA", "BB", "BC", "BD", "BE"), class = "factor"), location = c(10045L,
12041L, 15035L, 22054L, 19023L, 49411L, 39012L, 3041L, 23065L,
33015L, 42069L, 26859L), category = structure(c(1L, 1L, 2L, 3L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L), .Label = c("X", "Y", "Z"), class = "factor"),
id = structure(c(1L, 8L, 2L, 7L, 6L, 10L, 5L, 1L, 1L, 3L,
4L, 9L), .Label = c("Apple", "Banana", "Cherry", "Grape",
"Mango", "Melon", "Orange", "Pear", "Raspberry", "Strawberry"
), class = "factor")), .Names = c("item", "location", "category",
"id"), class = "data.frame", row.names = c(NA, -12L))

my_data %>%
ggplot(aes(item, location, shape = category, label = id)) +
geom_col(aes(y = Inf), fill = "gray80") +
geom_point(size = 3) +
geom_text(vjust = -1) +
scale_x_discrete(expand = expand_scale(add = c(5,0))) +
coord_polar(theta = "y") +
theme_void()
If you want a break in the middle, you could change the item to a numeric value relating to it's desired position:
my_data %>%
mutate(item_pos = as.numeric(item),
item_pos = item_pos + if_else(item_pos > 5, 1, 0)) %>%
ggplot(aes(item_pos, location, shape = category, label = id)) +
...

Maybe you can work from this:
ggplot(data,aes(x=location, color=id, y=id)) +
geom_linerange(aes(y=id, xmin=0, xmax=50000, color=category), size=2, alpha=0.5) +
geom_point(size=3) +
coord_polar()

Related

R remove two sides of border from strip.background function

Dataframe
df <- data.frame(
structure(list(biological_group = structure(1:15, .Label = c("A",
"B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O"), class = "factor"), norm_expression = c(2L, 3L, 4L, 6L,
1L, 5L, 7L, 8L, 9L, 3L, 2L, 6L, 7L, 8L, 1L), SE = c(0.171499719,
0.089692493, 0.153777208, 0.188012958, 0.153776128, 0.192917199,
0.224766056, 0.231338325, 0.121716906, 0.094763028, 0.09635363,
0.069986333, 0.113681329, 0.094614957, 0.391182473), Group = structure(c(1L,
1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("",
"Plant Products", "Sugars"), class = "factor")), class = "data.frame", row.names = c(NA,
-15L), .Names = c("biological_group", "norm_expression", "SE",
"Group")))
Sample code
library(ggplot2)
DFplot <- ggplot(df, aes(biological_group,norm_expression)) +
ylab("Relative Normalized\nExpression (∆∆Cq)") +
geom_bar(fill="black",stat="identity") +
theme(axis.title.x = element_blank())
DFplot2 <- DFplot+geom_errorbar(aes(ymin=norm_expression-SE,ymax=norm_expression+SE),width=0.5) +
geom_boxplot() +
facet_grid(~Group, scales = "free_x", switch = "x", space = "free_x") +
scale_y_continuous(expand = c(0,0), labels = scales::number_format(accuracy = 0.1)) +
theme_classic()
That gives me this graph:
I'd like to remove the vertical lines from the strip text labels (specified by strip.background), like this:
I realize I could just use photoshop or something, but I have several graphs to make so it would be easier if I could just specify it in the code.
This answer was helpful.
In your case, you want to find the strip-b for your bottom strips to substitute.
Edit: Replaced top bar of bottom strip that was removed accidentally.
library(grid)
q <- ggplotGrob(DFplot2)
lg <- linesGrob(x=unit(c(1,0),"npc"), y=unit(c(1,1),"npc"), gp=gpar(col="black", lwd=2))
for (k in grep("strip-b",q$layout$name)) {
q$grobs[[k]]$grobs[[1]]$children[[1]] <- lg
}
grid.draw(q)

Credibility interval with respect two factors using ggplot2 in r

I have problem ploting credibility interval like this:
My data structure is following,L1,L2,M,U1,U2 stand for 0.025quant,0.25quant,0.5quant,0.75quant,0.975quant,respectively.
`
structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")`
I referenced this answerenter link description here,but 'fill' seems only work for boxplot case.the code I tried so far is:
CI$AP=interaction(CI$approach,CI$param)
p=ggplot(CI,aes(y=AP))+geom_point(aes(x=M))
p=p+geom_segment(aes(x=L1,xend=U1,y=AP,yend=AP))
p=p+geom_segment(aes(x=L2,xend=U2,y=AP,yend=AP),size=1.5)
It is far away from what I want.
Many thanks!
How about the following:
ggplot(df, aes(x = param, y = M, colour = approach)) +
geom_point(position = position_dodge2(width = 0.3), size = 3) +
geom_linerange(
aes(ymin = L2, ymax = U2, x = param),
position = position_dodge2(width = 0.3),
size = 2) +
geom_linerange(
aes(ymin = L1, ymax = U1, x = param),
position = position_dodge2(width = 0.3),
size = 1) +
coord_flip() +
labs(x = "Parameter", y = "Estimate")
Sample data
df <- structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")

ggplot change color of one bar from stacked bar chart

Is there way to change colors of one bar( x - value) manualy in ggplot
data
for_plot_test=structure(list(name = c("A", "B",
"C", "A1", "A2", "A3",
"A4", "BI", "A", "B",
"C", "A1", "A2", "A3",
"A4", "BI"), n = c(1L, 3L, 5L, 7L, 9L, 11L, 13L, 15L, 2L, 4L, 6L, 8L, 10L, 12L, 14L, 16L),
value = c(0, 0.05, 0, 0.05, 0.05, 0.1, 0.05, 0, 1, 0.7, 0.6, 0.5, 0.4, 0.2, 0.2, 0.1),
variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
.Label = c("PROGRESS", "prev_progress"), class = "factor")),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -16L), vars = "name", labels = structure(list(name = c("Applications", "BI", "Clients", "CRE & Scoring", "Portfolio & Production", "SG Russia", "Transactions", "УКЛ & Prescoring")),
row.names = c(NA, -8L), class = "data.frame", vars = "name", drop = TRUE,
indices = list(0:1, 14:15, 6:7, 10:11, 2:3, 12:13, 8:9, 4:5),
group_sizes = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
biggest_group_size = 2L, .Names = "name"),
indices = list(c(0L, 8L), c(7L, 15L), c(3L, 11L), c(5L, 13L), c(1L, 9L), c(6L, 14L), c(4L, 12L), c(2L, 10L)),
drop = TRUE, group_sizes = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), biggest_group_size = 2L,
.Names = c("name", "n", "value", "variable"))
Current plot
colot_progress=c("#be877a","#dcbfad")
s <- ggplot(for_plot_test, aes(x= reorder(name, -n),y = value, fill = variable,label=ifelse(for_plot$value==0,"",scales::percent(for_plot$value))))+
geom_bar(stat='identity',position = "stack")+
scale_fill_manual(values=colot_progress,aesthetics = "fill")+
coord_flip()+
theme_minimal() + theme(
axis.title = element_blank(),
axis.text.x=element_blank(),
panel.grid = element_blank(),
legend.position="none"
)+
geom_text(size = 5, position = position_stack(vjust = 0.5))
s
Illustration of desire result
Creating another level for the column variable.
library(dplyr)
for_plot_test1 <-
for_plot_test %>%
group_by(name) %>%
summarise(n = n()) %>%
mutate(value = ifelse(name == "A", 1, 0), variable = "dummy") %>%
full_join(for_plot_test %>% mutate(value = replace(value, name == "A", 0)))
for_plot_test1$variable <- factor(for_plot_test1$variable,
levels = c("dummy", "PROGRESS", "prev_progress"))
colot_progress <- c("limegreen", "#be877a", "#dcbfad")
s <- ggplot(for_plot_test1,
aes(
x = reorder(name,-n),
y = value,
fill = variable,
label = ifelse(value == 0, "", scales::percent(value))
)) +
geom_bar(stat = 'identity', position = "stack") +
scale_fill_manual(values = colot_progress, aesthetics = "fill") +
coord_flip() +
theme_minimal() + theme(
axis.title = element_blank(),
axis.text.x = element_blank(),
panel.grid = element_blank(),
legend.position = "none"
) +
geom_text(size = 5, position = position_stack(vjust = 0.5))
s

plotting with different color between old and updated data

I reformulated my question and hope it is a bit clearer now:
Here is my data:
ID Type X Y Sex
a1 Test -12.12609861 208.6810478 XY
a2 Test -1.32366642 63.0574351 XXY
a3 Test -9.02867948 114.1501293 XY
b4 NewTest 0.01101428 0.87207664 XX
b5 Test -1.14651604 -0.86714741 XX
b6 Test -13.05848944 155.5109551 XY
x7 NewTest -4.74479593 80.82528931 XY
x8 Test -8.17386444 124.4765311 XY
x9 Test 1.14870262 -0.36606683 XX
x10 Test 1.20879037 0.80972607 XX
x11 Test -1.04261274 0.35654895 XX
x12 Test -11.73602 185.5326725 XY
I would like to plot the data with different color according to whether the data is new or old.
The new data is added daily or weekly so the color change needs to be dynamic. N.B the new data always start with "newTest" in the column "TYPE"
The code:
for_loop_start<- (nrow(whole_data)-1)
len_of_whole_data<- nrow(whole_data)
for (j in c(for_loop_start:1)){
if (whole_data[j,2] == "NewTest"){
break
}
}
new_data <- with(whole_data,whole_data[j:len_of_whole_data,])
> p <- ggplot(data=whole_data,aes(x=X,y=Y)) + geom_point(colour = "black")
> ggplotly(p)
> p <- p + geom_point(data= new_data,
mapping=aes(x=X,y=Y,text=SampleID,colour = "darkgoldenrod2"))
> ggplotly(p)
Answer to edited version of the question
If the last "NewType" value in your "Type" column consistently starts "new data", this should work:
dat <- structure(list(ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 10L,
11L, 12L, 7L, 8L, 9L), .Label = c("a1", "a2", "a3", "b4", "b5",
"b6", "x10", "x11", "x12", "x7", "x8", "x9"), class = "factor"),
Type = structure(c(2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 2L), .Label = c("NewTest", "Test"), class = "factor"),
X = c(-12.12609861, -1.32366642, -9.02867948, 0.01101428,
-1.14651604, -13.05848944, -4.74479593, -8.17386444, 1.14870262,
1.20879037, -1.04261274, -11.73602), Y = c(208.6810478, 63.0574351,
114.1501293, 0.87207664, -0.86714741, 155.5109551, 80.82528931,
124.4765311, -0.36606683, 0.80972607, 0.35654895, 185.5326725
), Sex = structure(c(3L, 2L, 3L, 1L, 1L, 3L, 3L, 3L, 1L,
1L, 1L, 3L), .Label = c("XX", "XXY", "XY"), class = "factor")), .Names = c("ID",
"Type", "X", "Y", "Sex"), class = "data.frame", row.names = c(NA,
-12L))
lim.id <- max(which(dat$Type == "NewTest")) - 1
dat$Age <- c(rep("old", lim.id), rep("new", nrow(dat) - lim.id))
ggplot(dat, aes(x=X, y=Y, color = Age)) + geom_point() +
scale_color_manual(values = c("darkgoldenrod2", "black"))
Old answer
You could try to make a script that reads the modification time (see ?file.mtime) and use that to make a column, which specifies whether the entry is "new" or "old"
dat <- structure(list(ID = 1:12, Type = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("control", "Test"
), class = "factor"), X = c(-12.12609861, -1.32366642, -9.02867948,
0.01101428, -1.14651604, -13.05848944, -4.74479593, -8.17386444,
1.14870262, 1.20879037, -1.04261274, -11.73602), Y = c(208.6810478,
63.0574351, 114.1501293, 0.87207664, -0.86714741, 155.5109551,
80.82528931, 124.4765311, -0.36606683, 0.80972607, 0.35654895,
185.5326725), Sex = structure(c(3L, 2L, 3L, 1L, 1L, 3L, 3L, 3L,
1L, 1L, 1L, 3L), .Label = c("XX", "XXY", "XY"), class = "factor")), .Names = c("ID",
"Type", "X", "Y", "Sex"), class = "data.frame", row.names = c(NA,
-12L))
dat$Time <- seq(as.Date("2017-07-12"), as.Date("2017-06-12"), length = nrow(dat))
dat$Time.type <- ifelse(as.Date(Sys.time()) - dat$Time < 12, "new", "old")
library(ggplot2)
ggplot(dat, aes(x=X, y=Y, color = Time.type)) + geom_point() +
scale_color_manual(values = c("black", "darkgoldenrod2"))
you could also just set a variable which defines on which ID to split your dataframe for plotting (supposing df1 is your dataframe):
lim.id <- 7 #here you can put whatever value you would like to split your data.frame on
plot1 <- ggplot() +
geom_point(data = df1[df1$ID < lim.id, ], aes(x = X, y = Y), colour = "black")
plot1 <- plot1 +
geom_point(data = df1[df1$ID >= lim.id, ], aes(x = X, y = Y), colour = "darkgoldenrod2")
plot2 <- ggplotly(plot2)

With both stacked and dodged bars, how can you remove dodge-bar elements from 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.

Resources