So I managed to get this far...
ggplot(init, aes(x=factor(ANGLE), fill=NETWORK)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
facet_wrap(~SHOW) + opts(legend.position = "top") +
scale_y_continuous(labels = percent_format())
My problem is that the colored bars below represent the percent of ALL the camera ANGLE measurements for all the television programs in my study. For instance, the OREILLY graph has a bar that approaches 15% for ANGLE 2, which is %15 for all the ANGLE measurements in the chart, not solely those in the OREILLY facet. What I want each graph to show is the percentage of counts relative to just ONE television show (just that one facet), rather than all of them.
The idea is to compare the proportional use of camera angles among different shows, but with the way the graph is now, it is skewed to make the shows with more camera angle changes look as though they spend way more time at camera angle 2 than they actually do relative to the others.
The frustrating part of it all is that I spent an hour getting this to look the way I wanted, then I made the mistake of updating R. The packages updated along with it, and this happened.
A reduced size data table is available here.
EDIT: This doesn't work either. I tried putting "group=NETWORK" in either (and both) of the aes(..., ) terms, but nothing changed. I also tried the same thing with "group=SHOW", which I thought might have more of a chance since I wanted to get just one percentages for one SHOW in each facet (hence, the scales for each facet should go up to about 80% since so many of the shows are predominantly camera angle 2). Am I missing something?
ggplot(init, aes(x=factor(ANGLE), fill=NETWORK), group=SHOW)
+ geom_bar(aes(y = (..count..)/sum(..count..), group=NETWORK)) +
+ facet_wrap(~SHOW) + opts(legend.position = "top") +
+ scale_y_continuous(labels = percent_format())
Using the ..density.. stat rather than ..count.. seems to work for me:
ggplot(dat, aes(x=factor(ANGLE))) +
geom_bar(aes(y = ..density..,group = SHOW,fill = NETWORK)) +
facet_wrap(~SHOW) +
opts(legend.position = "top") +
scale_y_continuous(labels = percent_format())
At least, this produces a different result, I can't say for sure it reflects what you want. Additionally, I'm not sure why the ..count.. stat was behaving that way.
this is no longer working in newer versions of ggplot. The way to do it is now + stat_count(aes(y=..prop..))
Related
I know there are already at least two threads on this topic from several years ago, but I wonder if there have been any changes over the years that make new answers possible?:
Facet with free scales but keep aspect ratio fixed (9 years ago),
Mapping different states with geom_sf using facet wrap and scales free (5 years ago).
The question is: is it finally possible to make a facetted plot with free scale (to zoom on each single element) keeping axis-ratio fixed (in my case using coord_quickmap)?
Both these threads don't answers the question, or they just suggest using tmap instead of ggplot... But the point is: is there a way to go on with ggpot?
I tried two different ways to go on:
keeping aspect ratio but unable to zoom on each feature
lomb_prov <- read.csv("lomb_prov.csv")
ggplot(lomb_prov, aes(x = x, y = y)) +
geom_tile(fill = "light grey") +
facet_wrap(~ provincia) +
coord_quickmap()
setting scales = "free I'm able to zoom to each feature in facets but aspect ratio is lost
ggplot(lomb_prov, aes(x = x, y = y)) +
geom_tile(fill = "light grey") +
facet_wrap(~ provincia, scales = "free") +
coord_quickmap()
The outcame i would like to achive is to keep aspect ratio (like first plot) and zoom on each single province (like second plot).
So, anyone is able to suggest a way to combine both?
Waiting for kind suggestions, thank you!
ATTACHED FILE: https://drive.google.com/file/d/12bN8y22-AY2prsFq4VEHcdbqKZ6z4XEL/view?usp=sharing
I'm using ggplot and I get those weird horizontal lines out of geom_bar. I cannot provide a minimal working example: the same code works with few observations and it relies on data I am importing and transforming. However, I can show the relevant line of codes and cross my fingers someone ran into this issue:
ggplot(data) + geom_bar(aes(x=Horizon, y=Importance, fill=Groups),
position='fill', stat='identity') +
theme_timeseries2() +
scale_fill_manual(values=c('#1B9E77', 'orange2', 'black',
'red2', 'blue4')) +
xlab('') + ylab('')
My personal function, theme_timeseries2() isn't the source of the problem: it happens even if I stop after geom_bar. I checked for missing values in Importance and every other column of my data frame and there are none.
It's also very odd: the white lines aren't the same on the zoomed page as in the plot window of RStudio. They do print in .png format when I save the file, so there really is something going on with those horizontal bars. Any theory about why geom_bar() does this would be highly appreciated.
You can fix it by adding the fill as color. Like this:
geom_bar(aes(x=Horizon, y=Importance, fill=Groups, color=Groups),
position='fill', stat='identity')
This was suggested here.
I'm guessing the lines are due to a plotting bug between observations that go into each bar. (That could be related to the OS, the graphics device, and/or how ggplot2 interacts with them...)
I expect it'd go away if you summarized before ggplot2, e.g.:
library(dplyr);
data %>%
count(Horizon, Groups, wt = Importance, name = "Importance") %>%
ggplot() +
geom_col(aes(x = Horizon, y= Importance, fill = Groups), position = "fill") + ....
Mine went away when changing the size of the graphs in rmarkdown.
I have an ethogram-like ggplot where I plot the value of a factor quadrant (1 to 4), which is plotted for each frame of a movie (frameID). The color is given by 3 animals that are being tracked.
I am fairly satisfied with the graph but the amount of points makes it difficult to see, even with alpha. I was wondering how to add position_dodge in a way that doesn't destroy the plot.
ggplot(dataframe) ,
aes(frameID, quadrant, color=animal)) +
geom_jitter(alpha=0.5) +
scale_color_manual(values = c("#1334C1","#84F619", "#F43900")) +
theme_classic()+
theme(legend.position = 'none')
This link has useful info about dodging using geom_point.
R: How to spread (jitter) points with respect to the x axis?
I can change to geom_point with height, which works but it produces something awful.
+ geom_point(position = position_jitter(w = 0, h = 2))
Update
Data lives in GitHub
Lowering the alpha or changing size helps, adds trouble when rescaling the image.
https://github.com/matiasandina/MLA2_Tracking/blob/master/demo_data/sample_data.csv
Update 2022
It's been a while since I posted this initially, my original thoughts changed and are better reflected here, but I am still looking for a ggplot2 version of doing this!
I am a newbie to R and hence having some problems in plotting using ggplot and hence need help.
In the above diagram, if any of my bars have high values (in this case, a green one with value of 447), the plot and the plot title gets overlapped. The values here are normalised / scaled such that the y-axis values are always between 0-100, though the label might indicate a different number (this is the actual count of occurrences, where as the scaling is done based on percentages).
I would like to know how to avoid the overlap of the plot with the plot title, in all cases, where the bar heights are very close to 100.
The ggplot function I am using is as below.
my_plot<-ggplot(data_frame,
aes(x=as.factor(X_VAR),y=GROUP_VALUE,fill=GROUP_VAR)) +
geom_bar(stat="identity",position="dodge") +
geom_text(aes(label = BAR_COUNT, y=GROUP_VALUE, ymax=GROUP_VALUE, vjust = -1), position=position_dodge(width=1), size = 4) +
theme(axis.text.y=element_blank(),axis.text.x=element_text(size=12),legend.position = "right",legend.title=element_blank()) + ylab("Y-axis label") +
scale_fill_discrete(breaks=c("GRP_PERCENTAGE", "NORMALIZED_COUNT"),
labels=c("Percentage", "Count of Jobs")) +
ggtitle("Distribution based on Text Analysis 2nd Level Sub-Category") +
theme(plot.title = element_text(lineheight=1, face="bold"))
Here is the ggsave command, in case if that is creating the problem, with dpi, height and width values.
ggsave(my_plot,file=paste(paste(variable_name,"my_plot",sep="_"),".png",sep = ""),dpi=72, height=6.75,width=9)
Can anyone please suggest what need to be done to get this right?
Many Thanks
As Axeman suggests ylim is useful Have a look at the documentation here:
http://docs.ggplot2.org/0.9.3/xylim.html
In your code:
my_plot + ylim(0,110)
Also, I find this intro to axis quite useful:
http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/
Good luck!
From what I can find on stackoverflow, (such as this answer to using two scale colour gradients on one ggplot) this may not (yet) be possible with ggplot2.
I want to create a bubbleplot with two size aesthetics, one always larger than the other. The idea is to show the proportion as well as the absolute values. Now I could colour the points by the proportion but I prefer multi-bubbles. In Excel this is relatively simple. (http://i.stack.imgur.com/v5LsF.png) Is there a way to replicate this in ggplot2 (or base)?
Here's an option. Mapping size in two geom_point layers should work. It's a bit of a pain getting the sizes right for bubblecharts in ggplot though.
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(size = disp), shape = 1) +
geom_point(aes(size = hp/(2*disp))) + scale_size_continuous(range = c(15,30))
To get it looking most like your exapmle, add theme_bw():
P <- p + theme_bw()
The scale_size_continuous() is where you have to just fiddle around till you're happy - at least in my experience. If someone has a better idea there I'd love to hear it.