I'm creating an rcap graph in Stata and running into issues with the x-axis labels. No matter what I try, the labels fall outside the graph.
I want to push the labels (and the CI bars) closer together - in other words, I want to narrow the gaps between CI bars (and their associated labels) so that nothing gets cut out.
Here is the graph:
And here is my code:
set scheme cleanplots
twoway rcap medicaid_lo medicaid_hi status, xlabel(1 2 3, valuelabel noticks) ytitle("") xtitle("")
Thanks in advance!
set scheme cleanplots
input high low status
0.06 0.02 1
0.28 0.22 2
0.21 0.17 3
end
label define status 1"R" 2"NR" 3"Admin data"
label value status status
twoway rcap high low status, xlabel(1 2 3, valuelabel noticks) ytitle("") xtitle("") xscale(range(0.5 3.5))
You can also try using graphregion(margin(vlarge)), which increases the margins around your graph to allow for space for longer x-axis labels.
Related
I am trying to include the count labels on stacked bar plots which represent percentages. I want to show x-amount of individuals make up the graphed percentages. However, when I include the count labels my y-axis gets blown out of proportion because it changes to match the count data, not the percentages. Also, the bars are removed from the graph too? I have reviewed other posts similar to this, such as: "How to add percentage or count labels above percentage bar plot?". I cannot find the error in my r command.
My command used is as follows:
sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=speciesSci))+
geom_bar(position="fill",stat="identity")+
geom_text(aes(label=nDet),position=position_stack(vjust=0.5))+
theme(axis.text.x=element_text(angle=90,hjust=1))+
scale_y_continous(labels=scales::percent_format())
Example of data being graphed:
speciesSci recvDeployName nDet
1 Arenaria interpres Bucktoe Preserve 96
2 Arenaria interpres CHDE 132
3 Arenaria interpres Fortescue 22133
4 Arenaria interpres Mispillion 2031
5 Arenaria interpres Norbury 3709
6 Arenaria interpres Penn - DRL 49
What my graph looks like when I use the command example provided above:
11/17/19 Update: The r commands below seems promising:
sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=speciesSci))+
geom_text(aes(label=nDet),position="fill", stat="identity")+
theme(axis.text.x=element_text(angle=90,hjust=1))+
scale_y_continuous(labels=scales::percent_format())
I just need to get the colored bars back onto the graph, representing the percentages.
Any help would be greatly appreciated. Thank you.
This gave me the result I wanted:
sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=factor(speciesSci))+geom_bar(positon="fill", stat="identity")+geom_text(aes(label=nDet),position = position_fill(vjust=0.5))+theme(axis.text.x=element_text(angle=90,hjust=1))+scale_y_continuous(labels=scales::percent_format()))
Correct Graph
I made a bar plot with three slope estimates, including their standard errors. However, one of these estimates are from another model than the other two. Therefore I would like a gap or a line or so in between so that it is clear they are from different models. Can anyone help me with that?
My code and my data look like this:
ggplot(data=data, aes(x=Name, y=Slope))+geom_bar(stat="identity")+geom_errorbar(aes(x=data$Name, ymin=(data$Slope-data$SE_Slope), ymax=(data$Slope+data$SE_Slope), width=.25))
Name Slope SE_Slope
BP 1.72 0.43
AP 1.74 0.51
DIF 1.70 0.46
geom_vline() may be of use to you! It might work funny with the x axis with geom_bar, but this may help:
Add a vertical line with ggplot when x-axis is a factor
Additionally, you could use annotate() if you really want to point it out.
http://ggplot2.tidyverse.org/reference/annotate.html
I have a data
equip ballots freq %Undercounts
1 LEVER 427780 17016 3.98
2 OPTICAL 1436159 39090 2.72
3 PAPER 3454 113 3.27
4 PUNCH 823921 38462 4.67
I want to plot equipment on the X-axis. And ballots should be at 100% for each equip. Freq should be the percentage of the bars and be represented as a percentage of ballots.
I have tried barplot and lattice plots. But I'm getting some error which seems like my data is not in form. Can someone please help me in what I might be missing.
Thanks.
It is very simple to accomplish with lattice
barchart(freq/ballots + (ballots-freq)/ballots ~ equip, tab, stack=TRUE)
Base graphics require more code and some obscure parameter tweaking to look good
attach(tab)
barplot(rbind(freq/ballots, 1), width=.8, space=.25)
mtext(equip, side=1, at=1:nrow(tab)-.4)
I would like to increase the size of my margins without altering the size of my plot region. What I'm doing is:
plotting my data, then querying the values of mai (margin size in inches) and fin (figure region dimension in inches).
adjusting mai by one inch at bottom and left.
adjusting fin by one inch for width and one inch for height.
re-plotting data.
However, I always get the error:
Error in plot.new() : figure region too large
Can anyone help? There's obviously something I don't understand about how mai and fin interact.
Here is an example:
x=c(1,2)
y=c(2,4)
plot(x,y)
init_fin <- par("fin") #gives 7 7
init_mai <- par("mai") #gives 1.02 0.82 0.82 0.42
final_fin <- init_fin + 1 #now 8 8
par(mai=c(2.02,1.82,0.82,0.42), fin=final_fin)
plot(x,y)
ERROR:
Error in plot.new() : figure region too large
this error depends on the device you're using, I see it all the time with Rstudio because the plot panel is quite small on my laptop screen, and R thinks there's not enough space to display the graphic. One option is to open a bigger device window explicitly, e.g.
dev.new(width=10, height=10)
just before your last plot.
Use
par(mai = c(bottom, left, top, right)) before the plot. It will create extra space around the plot area. Change values of bottom, left, top and right according to your taste.
I'd like to make just a rug plot where each series is plotted as a horizontal band of ticks, similar to the plot shown on this page. I've seen this called a "Raster Plot" as well. The rug plot looks similar but it looks like geom_rug only works for the margins. Can I get a raster plot in GGplot2? If not, is there a way to do it in other R plotting packages? Obviously the package I linked could do this, but I'd prefer a reference to a pure graphical package and not a scientific analysis tool.
Update: my data would look like this:
Time Series Value
0.01 A 1.5
0.01 B 0.78
0.5 C 1.25
0.75 A 1.5
0.76 B 1.6
0.76 C 0.2
1 C 1.2
What I would expect is three vertical bands representing series A, B and C (y axis) and the x axis would be timescaled. For each series, at each timepoint I would see the tick-mark. The Value is not important to me at this time. I just want a visible indicator that Series X showed an event at time T.
You could use the geom_point() and set shape to "|" to produce vertical bands.
ggplot(mtcars,aes(wt,cyl))+geom_point(shape="|",size=7)