Posts

Showing posts with the label Ggplot2

Cluster Data In Heat Map In R Ggplot

Image
Answer : You can achieve this by defining the order of Timepoints in a dendrogram after you have applied hclust to your data: data <- scale(t(data)) ord <- hclust( dist(data, method = "euclidean"), method = "ward.D" )$order ord [1] 2 3 1 4 8 5 6 10 7 9 The only thing you have to do then is transforming your Time-column to a factor where the factor levels are ordered by ord : pd <- as.data.frame( data ) pd$Time <- sub("_.*", "", rownames(pd)) pd.m <- melt( pd, id.vars = "Time", variable.name = "Gene" ) pd.m$Gene <- factor( pd.m$Gene, levels = colnames(data), labels = seq_along( colnames(data) ) ) pd.m$Time <- factor( pd.m$Time, levels = rownames(data)[ord], labels = c("0h", "0.25h", "0.5h","1h","2h","3h","6h","12h","24h","48h") ) The rest is done by ggplot automatically: ggplot( pd....

Bar Graph From Dataframe Groupby

Image
Answer : copying data from your link and running df = pd.read_clipboard() then using your code df = df.replace(np.nan,0) df = df.groupby(['home_team'])['arrests'].mean() df.plot.bar() Good one by @piRSuared, and I just buitified his answer :) ## referenced to the answer by @piRSquared df = df.replace(np.nan,0) df = df.groupby(['home_team'])['arrests'].mean() ax = df.plot(kind='bar', figsize=(10,6), color="indigo", fontsize=13); ax.set_alpha(0.8) ax.set_title("My Bar Plot", fontsize=22) ax.set_ylabel("Some Heading on Y-Axis", fontsize=15); plt.show()

Add A Horizontal Line To Plot And Legend In Ggplot2

Image
Answer : (1) Try this: cutoff <- data.frame( x = c(-Inf, Inf), y = 50, cutoff = factor(50) ) ggplot(the.data, aes( year, value ) ) + geom_point(aes( colour = source )) + geom_smooth(aes( group = 1 )) + geom_line(aes( x, y, linetype = cutoff ), cutoff) (2) Regarding your comment, if you don't want the cutoff listed as a separate legend it would be easier to just label the cutoff line right on the plot: ggplot(the.data, aes( year, value ) ) + geom_point(aes( colour = source )) + geom_smooth(aes( group = 1 )) + geom_hline(yintercept = 50) + annotate("text", min(the.data$year), 50, vjust = -1, label = "Cutoff") Update This seems even better and generalizes to mulitple lines as shown: line.data <- data.frame(yintercept = c(50, 60), Lines = c("lower", "upper")) ggplot(the.data, aes( year, value ) ) + geom_point(aes( colour = source )) + geom_smooth(aes( group ...

Center Plot Title In Ggplot2

Image
Answer : From the release news of ggplot 2.2.0 : "The main plot title is now left-aligned to better work better with a subtitle". See also the plot.title argument in ?theme : "left-aligned by default". As pointed out by @J_F, you may add theme(plot.title = element_text(hjust = 0.5)) to center the title. ggplot() + ggtitle("Default in 2.2.0 is left-aligned") ggplot() + ggtitle("Use theme(plot.title = element_text(hjust = 0.5)) to center") + theme(plot.title = element_text(hjust = 0.5)) As stated in the answer by Henrik, titles are left-aligned by default starting with ggplot 2.2.0. Titles can be centered by adding this to the plot: theme(plot.title = element_text(hjust = 0.5)) However, if you create many plots, it may be tedious to add this line everywhere. One could then also change the default behaviour of ggplot with theme_update(plot.title = element_text(hjust = 0.5)) Once you have run this line, all plots created...